文档如下:
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也是。
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也是。
第6行,$audioType = 'lame';
34行 TODO :
$save=fopen($saveName,'w');
fwrite($save,$result);
fclose($save);
放出来了
这个是运行结果
以下是代码:
<?phpclass 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");