php 接入百度语音的识别的api
程序员文章站
2022-05-19 17:32:33
...
"wav",
"rate"=>8000,
"channel"=>1,
"token"=>$token,
"cuid"=>"74-D0-2B-BE-5E-31",
"url"=>$pic_path,
"callback" =>"XXXX",
//"key" => $appkey,//APP Key
//"file" => $pic_path,//上传的音频文件(采样率16k或者8k(rate),采样精度16bit(bit),单声道的pcm或者wav格式的音频)
//"rate" => "8000",//采样率(16000或者8000)
//"pname" => date("YmdHis"),//包名
//"device_id" => "",//标记请求来源的标识,如用户所设备序列号 (SN),IMEI,MAC地址等
);
//$paramstring = http_build_query($params);
$paramstring=json_encode($params);
$content = juhecurl($url,$paramstring,1);
$result = json_decode($content,true);
if($result){
//if($result['error_code']=='0'){
print_r($result);
//}else{
// echo $result['error_code'].":".$result['reason'];
//}
}else{
echo "请求失败";
}
/**
* 请求接口返回内容
* @param string $url [请求的URL地址]
* @param string $params [请求的参数]
* @param int $ipost [是否采用POST形式]
* @return string
*/
function juhecurl($url,$params=false,$ispost=0){
$size=strlen($params);
echo $size;
$header = array(
"Content-Type: application/json",
"Content-length:" . $size,
);
$httpInfo = array();
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //设置头信息的地方
curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if( $ispost )
{
curl_setopt( $ch , CURLOPT_POST , true );
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
curl_setopt( $ch , CURLOPT_URL , $url );
}
else
{
if($params){
curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
}else{
curl_setopt( $ch , CURLOPT_URL , $url);
}
}
$response = curl_exec( $ch );
if ($response === FALSE) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
curl_close( $ch );
return $response;
}
?>
有人用过吗,是提供url的方式的那种,为什么一直提示的是Array
(
[err_msg] => parameters error.
[err_no] => 3300
[sn] => 967126290621465557773
)
回复内容:
"wav",
"rate"=>8000,
"channel"=>1,
"token"=>$token,
"cuid"=>"74-D0-2B-BE-5E-31",
"url"=>$pic_path,
"callback" =>"XXXX",
//"key" => $appkey,//APP Key
//"file" => $pic_path,//上传的音频文件(采样率16k或者8k(rate),采样精度16bit(bit),单声道的pcm或者wav格式的音频)
//"rate" => "8000",//采样率(16000或者8000)
//"pname" => date("YmdHis"),//包名
//"device_id" => "",//标记请求来源的标识,如用户所设备序列号 (SN),IMEI,MAC地址等
);
//$paramstring = http_build_query($params);
$paramstring=json_encode($params);
$content = juhecurl($url,$paramstring,1);
$result = json_decode($content,true);
if($result){
//if($result['error_code']=='0'){
print_r($result);
//}else{
// echo $result['error_code'].":".$result['reason'];
//}
}else{
echo "请求失败";
}
/**
* 请求接口返回内容
* @param string $url [请求的URL地址]
* @param string $params [请求的参数]
* @param int $ipost [是否采用POST形式]
* @return string
*/
function juhecurl($url,$params=false,$ispost=0){
$size=strlen($params);
echo $size;
$header = array(
"Content-Type: application/json",
"Content-length:" . $size,
);
$httpInfo = array();
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //设置头信息的地方
curl_setopt( $ch, CURLOPT_USERAGENT , 'JuheData' );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
if( $ispost )
{
curl_setopt( $ch , CURLOPT_POST , true );
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
curl_setopt( $ch , CURLOPT_URL , $url );
}
else
{
if($params){
curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
}else{
curl_setopt( $ch , CURLOPT_URL , $url);
}
}
$response = curl_exec( $ch );
if ($response === FALSE) {
//echo "cURL Error: " . curl_error($ch);
return false;
}
$httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
$httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
curl_close( $ch );
return $response;
}
?>
有人用过吗,是提供url的方式的那种,为什么一直提示的是Array
(
[err_msg] => parameters error.
[err_no] => 3300
[sn] => 967126290621465557773
)
err_msg提示你参数错误,err_no说错误代码是3300,去百度语音识别 - 开发文档找这个错误代码,可以看到:
错误码 含义
3300 输入参数不正确
建议用抓包工具看一下PHP发出的请求是不是和文档上的相符
上一篇: PHP 批量图片缩放预览有关问题
推荐阅读