PHP code example of simonetoo / coze

1. Go to this page and download the library: Download simonetoo/coze library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

simonetoo / coze example snippets




imonetoo\Coze\Coze;

// 使用API密钥初始化客户端
$client = new Coze([
    'token' => 'your_api_key_here',
]);



use Simonetoo\Coze\Coze;
use Simonetoo\Coze\Exceptions\ApiException;

try {
    $client = new Coze([
        'token' => 'your_api_key_here',
    ]);
    
    // 执行API调用
    $response = $client->bot()->list();
    
    // 处理响应
    $bots = $response->json();
    
} catch (ApiException $e) {
    // 处理API错误
    echo "API错误: " . $e->getMessage();
    echo "HTTP状态码: " . $e->getCode();
    
    // 获取更多错误详情(如果有)
    if ($e->hasResponse()) {
        $errorDetails = $e->getResponse()->json();
        echo "错误详情: " . json_encode($errorDetails);
    }
} catch (\Exception $e) {
    // 处理其他错误
    echo "错误: " . $e->getMessage();
}

$response = $client->bot()->list();
$bots = $response->json();

$botId = '7484523878849822754';
$response = $client->bot()->retrieve($botId);
$botInfo = $response->json();

$filePath = '/path/to/your/file.jpg';
$response = $client->file()->upload($filePath);
$fileInfo = $response->json();
$fileId = $fileInfo['file_id'];

$fileId = '7484881587176734755';
$response = $client->file()->retrieve($fileId);
$fileInfo = $response->json();

// 语音合成示例
$voiceId = '7468518753626652709'; // 音色ID
$text = '这是一个测试文本,用于演示语音合成功能。';

$response = $client->audio()->speech($voiceId, $text);

// 保存生成的音频文件
$audioFile = '/path/to/save/audio.mp3';
file_put_contents($audioFile, $response->getBody());

echo "语音合成成功,文件已保存到: $audioFile\n";

// 语音识别示例
$audioPath = '/path/to/your/audio.mp3';
$response = $client->audio()->transcription($audioPath);

// 输出识别结果
$result = $response->json();
echo "识别文本: " . $result['text'] . "\n";
echo "音频时长: " . $result['duration'] . " 秒\n";
echo "识别语言: " . $result['language'] . "\n";

// 获取音色列表示例
$response = $client->voice()->list();
$voices = $response->json()['voices'];

// 输出所有可用音色
foreach ($voices as $voice) {
    echo "ID: " . $voice['id'] . "\n";
    echo "名称: " . $voice['name'] . "\n";
    echo "描述: " . $voice['description'] . "\n";
    echo "语言: " . $voice['language'] . "\n";
    echo "------------------------\n";
}

// 复刻音色示例
$audioPath = '/path/to/sample/audio.mp3';
$voiceName = '自定义音色名称';

$response = $client->voice()->clone($voiceName, $audioPath, [
    'preview_text' => '这是一个用于预览的文本示例。',
]);

// 输出结果
$result = $response->json();
echo "音色ID: " . $result['voice_id'] . "\n";
echo "音色名称: " . $result['name'] . "\n";
echo "状态: " . $result['status'] . "\n";

$response = $client->workspace()->list();
$workspaces = $response->json();

$workspaceId = '7484524201249194023';
$response = $client->knowledge()->list($workspaceId);
$knowledgeBases = $response->json();

$response = $client->bot()->list();

// 获取JSON数据
$json = $response->json();

// 根据path获取数据
$item = $response->json('data.item', 'default');

// 获取code
$code = $response->code();

// 获取数据
$data = $response->data();
// 根据path获取数据
$item = $response->data('item', 'default')

// 获取HTTP状态码
$statusCode = $response->getStatusCode();

// 获取响应头
$headers = $response->getHeaders();

$response = $client->audio()->speech('voice_id', 'Hello world');

// 保存到文件
file_put_contents('output.mp3', $response->getBody());

try {
    $response = $client->bot()->retrieve('invalid_id');
} catch (\Simonetoo\Coze\Exceptions\ApiException $e) {
    echo "API错误: " . $e->getMessage();
    echo "HTTP状态码: " . $e->getCode();
    
    if ($e->hasResponse()) {
        $errorDetails = $e->getResponse()->json();
        echo "错误详情: " . json_encode($errorDetails);
    }
}