讯飞语音接口用php接入,如何把response body的音频数据保存为文件

zxh_33 zxh_33 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
文档如下:
http://doc.xfyun.cn/rest_api/%E8%AF%AD%E9%9F%B3%E5%90%88%E6%88%90.html

本地代码如下,可以正常获得返回:

<?php
//用户参数
$apikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$appid = 'xxxxxxx';
$CurTime = (string)time();
$audioType = 'raw';
//语音参数
$json = array('aue' => $audioType, 'auf' => 'audio/L16;rate=16000', 'voice_name' => 'xiaoyan');
$param = base64_encode(utf8_encode(json_encode($json, JSON_UNESCAPED_SLASHES)));
$body = 'text=讯飞是中国最大的智能语音技术提供商';
//验证参数
$checkSum = md5($apikey . $CurTime . $param);
//http头
$header = array(
'X-Appid:' . $appid,
'X-CurTime:' . $CurTime,
'X-Param:' . $param,
'X-CheckSum:' . $checkSum,
);
//请求
$url = 'http://api.xfyun.cn/v1/service/v1/tts';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$result = curl_exec($ch);
curl_close($ch);
//处理响应
echo($result);
$saveType = $audioType == 'raw' ? '.wav' : '.mp3';
$saveName = $CurTime . $saveType;
// TODO:save wav file


echo结果:


文档示例是python,response.content可以直接获取,PHP有没有类似方法呢。直接写$result放不出来,CURLOPT_HEADER,false也是。
...全文
评论 4
收藏 已收藏
分享
返回顶部
zxh_33
基础编程 21,889 成员 140,329 内容