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

zxh_33 2018-04-03 04:42:19
文档如下:
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也是。
...全文
2402 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_32282331 2018-04-23
  • 打赏
  • 举报
回复
您好,我看您也是做的这一块,想向您咨询个问题,那个我要做文本理解,运用PHP webapi ,运行以后出现这个结果,不能成功返回,想让您帮忙看看是什么原因?
这个是运行结果
以下是代码:
<?php

class AIUI
{
private $m_appid = " ";
private $m_apikey = " ";
private $m_heads = [];
private $m_debug = true;
private $m_checkSum = [];
private $m_body = "";
private $m_host = "http://api.xfyun.cn";

public function setDebug($debug)
{
$this->m_debug = $debug;
}

public function setBody($body)
{
$this->m_body = $body;
}

public function setHead($scene="main", $userid = "user_0001", $auf="", $aue="")
{
$this->m_heads =[];
$CurTime = time();
$this->m_heads[] = "X-Appid: {$this->m_appid}";
$this->m_heads[] = "X-CurTime: {$CurTime}";
$Param = [];
$Param["userid"] = $userid;
$Param["scene"] = $scene;
if ($auf != "") {
$Param["auf"] = $auf;
}
if ($aue != "") {
$Param["aue"] = $aue;
}

$Param = json_encode($Param, JSON_UNESCAPED_UNICODE);
if ($this->m_debug) {
var_dump("X-Param:".$Param."<br/>");
}
$Param = base64_encode($Param);
if ($this->m_debug) {
var_dump("X-Param(base64):".$Param."<br/>");
}
$this->m_heads[] = "X-Param: {$Param}";

$this->m_checkSum[] = $this->m_apikey;
$this->m_checkSum[] = $CurTime;
$this->m_checkSum[] = $Param;
$this->m_checkSum[] = $this->m_body;

$checkSum = join($this->m_checkSum, "");
if ($this->m_debug) {
var_dump("X-CheckSum:".$checkSum."<br/>");
}
$checkSum = md5($checkSum);
if ($this->m_debug) {
var_dump("X-CheckSum(md5):".$checkSum."<br/>");
}
$this->m_heads[] = "X-CheckSum: {$checkSum}";
$this->m_heads[] = "Content-type: application/x-www-form-urlencoded; charset=utf-8";
$this->m_heads[] = "Accept: */*";
$this->m_heads[] = "Content-Length: ".strlen($this->m_body);
}

public function request_text_semantic($text)
{
if ($this->m_debug) {
var_dump("X-Body:".$text."<br/>");
}
//$text = iconv("utf-8","gb2312",$text);
$text = base64_encode($text);
if ($this->m_debug) {
var_dump("X-Body(base64):".$text."<br/>");
}

$this->setBody("text={$text}");
$this->setHead();
$ch = curl_init();
//设置抓取的url 文本语义接口
curl_setopt($ch, CURLOPT_URL, "{$this->m_host}/v1/aiui/v1/text_semantic");
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->m_heads);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->m_body);

$data = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($data, true));
exit();
}

public function base64_encode_file($file)
{
if (!file_exists($file)) {
return "";
}
$fp = fopen($file, 'rb');
$content = fread($fp, filesize($file));
fclose($fp);
$content = base64_encode($content);
return $content;
}
}

$aiui = new AIUI();
$aiui->request_text_semantic("今天星期几");
//$aiui->request_voice_semantic("C:\\Users\\user\\Desktop\\程序日记\\讯飞语音\\16kVoice.pcm");
zxh_33 2018-04-04
  • 打赏
  • 举报
回复
mp3把返回头一起写进去也可以正常播放

但是wav格式写进返回头就放不出来。想要存wav格式就要类似python的response.content方法,php中如何实现?
zxh_33 2018-04-03
  • 打赏
  • 举报
回复
引用 1 楼 zxh_33 的回复:
刚刚发现,改成返回mp3格式就可以了 第6行,$audioType = 'lame'; 34行 TODO : $save=fopen($saveName,'w'); fwrite($save,$result); fclose($save); 放出来了
所以为什么wav不行
zxh_33 2018-04-03
  • 打赏
  • 举报
回复
刚刚发现,改成返回mp3格式就可以了 第6行,$audioType = 'lame'; 34行 TODO : $save=fopen($saveName,'w'); fwrite($save,$result); fclose($save); 放出来了

21,893

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧