Home > Backend Development > PHP Tutorial > PHP accesses Baidu voice recognition API

PHP accesses Baidu voice recognition API

WBOY
Release: 2016-07-06 13:53:53
Original
1504 people have browsed it

<code><?php

 //配置您申请的appkey
$token="XXXXX";//baidu

//************1.语音识别接口************
//$url = "http://japi.juhe.cn/voice_words/getWords";
$url="http://vop.baidu.com/server_api";
$params = array(
    "format"=>"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;
}
?></code>
Copy after login
Copy after login

Has anyone used it? It’s the way to provide a URL. Why does it always prompt Array
(

<code>[err_msg] => parameters error.
[err_no] => 3300
[sn] => 967126290621465557773</code>
Copy after login
Copy after login

)

Reply content:

<code><?php

 //配置您申请的appkey
$token="XXXXX";//baidu

//************1.语音识别接口************
//$url = "http://japi.juhe.cn/voice_words/getWords";
$url="http://vop.baidu.com/server_api";
$params = array(
    "format"=>"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;
}
?></code>
Copy after login
Copy after login

Has anyone used it? It’s the way to provide a URL. Why does it always prompt Array
(

<code>[err_msg] => parameters error.
[err_no] => 3300
[sn] => 967126290621465557773</code>
Copy after login
Copy after login

)

err_msg prompts you that the parameter is wrong, err_no says the error code is 3300, go to Baidu Speech Recognition - Development Documents to find this error code, you can see:

Error code meaning
3300 The input parameters are incorrect

It is recommended to use a packet capture tool to see if the request sent by PHP is consistent with the document

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template