How to Build a PHP SDK for Coze API When Official Support Is Missing
This guide explains why the official Coze SDK lacks PHP support, shows how to install the community‑made pfinalclub/coze_sdk package via Composer, and demonstrates chat, bot management, and both basic and advanced streaming features with complete code examples and future improvement plans.
Background
When trying to use Coze's API, the official documentation only provides SDKs for Python, Node.js, and Go, leaving PHP developers without a native client. Rather than switching languages, the author created a custom PHP SDK.
Installation
Install the package with a single Composer command: composer require pfinalclub/coze_sdk The package requires PHP 8.2 and the following extensions and libraries:
php: ^8.2
ext-http: *
ext-json: *
ext-openssl: *
firebase/php-jwt: ^6.10
psr/simple-cache: ^3.0
symfony/cache: ^7.1
symfony/http-client: ^7.1Current version is v1.0.0.
Feature Demonstrations
1. Chat Function
A simple chat example creates a chat instance, sets a user ID, sends a message, and iterates over the returned messages:
use CozeSdk\Chat\Chat;
// Create chat instance
$chat = new Chat($app);
// Send a message
$response = $chat->setUserId('user_123')
->sendMessage('你好,请介绍一下PHP');
// Process response
foreach ($response['messages'] as $message) {
echo $message['content'] . "
";
}2. Bot Management
Retrieve and list all bots associated with the account:
use CozeSdk\Bot\Bot;
$bot = new Bot($app);
$bots = $bot->getBots();
foreach ($bots as $bot) {
echo $bot['name'] . "
";
}3. Basic Streaming
Stream responses in real time, similar to a live broadcast:
use CozeSdk\Chat\Chat;
$chat = new Chat($app);
$streamCallback = $chat->setUserId('user_123')
->Query('请写一个PHP函数')
->Build(true);
$streamCallback();4. Advanced Streaming (Recommended)
The SDK provides several stream handlers for flexible processing:
<?php
use CozeSdk\Chat\Chat;
use CozeSdk\Kernel\Support\{ConsoleStreamHandler, JsonStreamHandler, MemoryStreamHandler, CallbackStreamHandler};
$chat = new Chat($app);
// 1. Console output – simplest
$consoleHandler = new ConsoleStreamHandler();
$chat->sendStreamMessage('你好,请介绍一下PHP', $consoleHandler);
// 2. JSON handler – suitable for API responses
$jsonHandler = new JsonStreamHandler();
$chat->sendStreamMessage('写一个简单的函数', $jsonHandler);
// 3. Memory handler – store data in memory for later use
$memoryHandler = new MemoryStreamHandler();
$chat->sendStreamMessage('解释什么是OOP', $memoryHandler);
// 4. Callback handler – fully custom processing
$callbackHandler = new CallbackStreamHandler(
function ($chunk, $metadata) {
echo $chunk;
flush();
return true; // continue processing
},
function ($metadata) {
echo "开始接收数据...
";
},
function ($metadata) {
echo "
接收完成
";
},
function ($error, $metadata) {
echo "错误: " . $error->getMessage() . "
";
}
);
$chat->sendStreamMessage('请写一个详细的教程', $callbackHandler);Usage Experience
The author found developing the package enjoyable and initially aimed only to solve a personal need, but the project grew. In current projects, the streaming feature powers a customer‑service bot, delivering responses in real time and improving user experience compared to waiting for a full reply.
Future Plans
Add more error‑handling mechanisms
Optimize performance and reduce memory usage
Provide additional example code
Complete documentation
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
