curl php 应用调用-DashScope API 流式输出 ,返回值中带有前面的结果

按照文档示例来的:// 构造请求数据
$data = [
"input" => [
'prompt' => $input],
"parameters" => [
'incremental_output' => true]];// 增量输出
// 将数据编码为 JSON
$dataString = json_encode($data);

    // 检查 json_encode 是否成功
    if (json_last_error() !== JSON_ERROR_NONE) {
        die("JSON encoding failed with error: " . json_last_error_msg());
    }

    // 初始化 cURL 对话
    $ch = curl_init($url);

    // 设置 cURL 选项
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); // 不返回传输的数据
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $string) {
        echo $string; // 处理流式数据
        return strlen($string);
    });
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Content-Type: application/json',
        'Authorization: Bearer ' . $api_key,
        'X-DashScope-SSE: enable' // 流式输出
    ]);

展开
收起
明教歌豪 2026-03-05 17:03:16 19 分享 版权
0 条回答
写回答
取消 提交回答

基于通义系列大模型和开源大模型的一站式大模型服务平台,提供「生成式大模型的全流程应用工具」和「企业大模型的全链路训练工具」。为大模型,也为小应用。 阿里云百炼官网网址:https://www.aliyun.com/product/bailian

还有其他疑问?
咨询AI助理