作者 何书鹏
1 个管道 的构建 通过 耗费 0 秒

语音转文字

... ... @@ -545,23 +545,17 @@ class Almighty extends Api
*/
public function asrJson($filename){
define('DEMO_CURL_VERBOSE', false); // 打印curl debug信息
# 填写网页上申请的appkey 如 $apiKey="g8eBUMSokVB1BHGmgxxxxxx"
$API_KEY = "kVcnfD9iW2XVZSMaLMrtLYIz";
# 填写网页上申请的APP SECRET 如 $secretKey="94dc99566550d87f8fa8ece112xxxxx"
$SECRET_KEY = "O9o1O213UgG5LFn0bDGNtoRN3VWl2du6";
# 需要识别的文件
$AUDIO_FILE = $filename;
# 文件格式
$FORMAT = substr($AUDIO_FILE, -3); // 文件后缀 pcm/wav/amr 格式 极速版额外支持m4a 格式
$CUID = "123456PHP";
# 采样率
$RATE = 16000; // 固定值
# 普通版
$ASR_URL = "http://vop.baidu.com/server_api";
# 根据文档填写PID,选择语言及识别模型
... ... @@ -578,10 +572,7 @@ class Almighty extends Api
//$SCOPE = 'brain_enhanced_asr'; // 有此scope表示有极速版能力,没有请在网页里开通极速版
$SCOPE = false; // 部分历史应用没有加入scope,设为false忽略检查
/** 公共模块获取token开始 */
$auth_url = "http://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=".$API_KEY."&client_secret=".$SECRET_KEY;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $auth_url);
... ... @@ -596,25 +587,16 @@ class Almighty extends Api
$this->error(curl_error($ch));
}
curl_close($ch);
echo "Token URL response is " . $res . "\n";
$response = json_decode($res, true);
if (!isset($response['access_token'])){
echo "ERROR TO OBTAIN TOKEN\n";
exit(1);
$this->error('ERROR TO OBTAIN TOKEN');
}
if (!isset($response['scope'])){
echo "ERROR TO OBTAIN scopes\n";
exit(2);
$this->error('ERROR TO OBTAIN scopes');
}
if ($SCOPE && !in_array($SCOPE, explode(" ", $response['scope']))){
echo "CHECK SCOPE ERROR\n";
// 请至网页上应用内开通语音识别权限
exit(3);
$this->error('CHECK SCOPE ERROR');// 请至网页上应用内开通语音识别权限
}
$token = $response['access_token'];
/** 公共模块获取token结束 */
... ... @@ -636,11 +618,9 @@ class Almighty extends Api
$json_array = json_encode($params);
$headers[] = "Content-Length: ".strlen($json_array);
$headers[] = 'Content-Type: application/json; charset=utf-8';
/** 拼接参数结束 **/
/** asr 请求开始 **/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ASR_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
... ... @@ -656,7 +636,8 @@ class Almighty extends Api
$this->error(curl_error($ch));
}
curl_close($ch);
/** asr 请求结束 **/
// 解析结果
$response = json_decode($res, true);
... ...