正在显示
1 个修改的文件
包含
5 行增加
和
24 行删除
@@ -545,23 +545,17 @@ class Almighty extends Api | @@ -545,23 +545,17 @@ class Almighty extends Api | ||
545 | */ | 545 | */ |
546 | public function asrJson($filename){ | 546 | public function asrJson($filename){ |
547 | define('DEMO_CURL_VERBOSE', false); // 打印curl debug信息 | 547 | define('DEMO_CURL_VERBOSE', false); // 打印curl debug信息 |
548 | - | ||
549 | # 填写网页上申请的appkey 如 $apiKey="g8eBUMSokVB1BHGmgxxxxxx" | 548 | # 填写网页上申请的appkey 如 $apiKey="g8eBUMSokVB1BHGmgxxxxxx" |
550 | $API_KEY = "kVcnfD9iW2XVZSMaLMrtLYIz"; | 549 | $API_KEY = "kVcnfD9iW2XVZSMaLMrtLYIz"; |
551 | - | ||
552 | # 填写网页上申请的APP SECRET 如 $secretKey="94dc99566550d87f8fa8ece112xxxxx" | 550 | # 填写网页上申请的APP SECRET 如 $secretKey="94dc99566550d87f8fa8ece112xxxxx" |
553 | $SECRET_KEY = "O9o1O213UgG5LFn0bDGNtoRN3VWl2du6"; | 551 | $SECRET_KEY = "O9o1O213UgG5LFn0bDGNtoRN3VWl2du6"; |
554 | - | ||
555 | # 需要识别的文件 | 552 | # 需要识别的文件 |
556 | $AUDIO_FILE = $filename; | 553 | $AUDIO_FILE = $filename; |
557 | # 文件格式 | 554 | # 文件格式 |
558 | - | ||
559 | $FORMAT = substr($AUDIO_FILE, -3); // 文件后缀 pcm/wav/amr 格式 极速版额外支持m4a 格式 | 555 | $FORMAT = substr($AUDIO_FILE, -3); // 文件后缀 pcm/wav/amr 格式 极速版额外支持m4a 格式 |
560 | - | ||
561 | $CUID = "123456PHP"; | 556 | $CUID = "123456PHP"; |
562 | # 采样率 | 557 | # 采样率 |
563 | $RATE = 16000; // 固定值 | 558 | $RATE = 16000; // 固定值 |
564 | - | ||
565 | # 普通版 | 559 | # 普通版 |
566 | $ASR_URL = "http://vop.baidu.com/server_api"; | 560 | $ASR_URL = "http://vop.baidu.com/server_api"; |
567 | # 根据文档填写PID,选择语言及识别模型 | 561 | # 根据文档填写PID,选择语言及识别模型 |
@@ -578,10 +572,7 @@ class Almighty extends Api | @@ -578,10 +572,7 @@ class Almighty extends Api | ||
578 | //$SCOPE = 'brain_enhanced_asr'; // 有此scope表示有极速版能力,没有请在网页里开通极速版 | 572 | //$SCOPE = 'brain_enhanced_asr'; // 有此scope表示有极速版能力,没有请在网页里开通极速版 |
579 | 573 | ||
580 | $SCOPE = false; // 部分历史应用没有加入scope,设为false忽略检查 | 574 | $SCOPE = false; // 部分历史应用没有加入scope,设为false忽略检查 |
581 | - | ||
582 | /** 公共模块获取token开始 */ | 575 | /** 公共模块获取token开始 */ |
583 | - | ||
584 | - | ||
585 | $auth_url = "http://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=".$API_KEY."&client_secret=".$SECRET_KEY; | 576 | $auth_url = "http://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=".$API_KEY."&client_secret=".$SECRET_KEY; |
586 | $ch = curl_init(); | 577 | $ch = curl_init(); |
587 | curl_setopt($ch, CURLOPT_URL, $auth_url); | 578 | curl_setopt($ch, CURLOPT_URL, $auth_url); |
@@ -596,25 +587,16 @@ class Almighty extends Api | @@ -596,25 +587,16 @@ class Almighty extends Api | ||
596 | $this->error(curl_error($ch)); | 587 | $this->error(curl_error($ch)); |
597 | } | 588 | } |
598 | curl_close($ch); | 589 | curl_close($ch); |
599 | - | ||
600 | - echo "Token URL response is " . $res . "\n"; | ||
601 | $response = json_decode($res, true); | 590 | $response = json_decode($res, true); |
602 | - | ||
603 | if (!isset($response['access_token'])){ | 591 | if (!isset($response['access_token'])){ |
604 | - echo "ERROR TO OBTAIN TOKEN\n"; | ||
605 | - exit(1); | 592 | + $this->error('ERROR TO OBTAIN TOKEN'); |
606 | } | 593 | } |
607 | if (!isset($response['scope'])){ | 594 | if (!isset($response['scope'])){ |
608 | - echo "ERROR TO OBTAIN scopes\n"; | ||
609 | - exit(2); | 595 | + $this->error('ERROR TO OBTAIN scopes'); |
610 | } | 596 | } |
611 | - | ||
612 | if ($SCOPE && !in_array($SCOPE, explode(" ", $response['scope']))){ | 597 | if ($SCOPE && !in_array($SCOPE, explode(" ", $response['scope']))){ |
613 | - echo "CHECK SCOPE ERROR\n"; | ||
614 | - // 请至网页上应用内开通语音识别权限 | ||
615 | - exit(3); | 598 | + $this->error('CHECK SCOPE ERROR');// 请至网页上应用内开通语音识别权限 |
616 | } | 599 | } |
617 | - | ||
618 | $token = $response['access_token']; | 600 | $token = $response['access_token']; |
619 | /** 公共模块获取token结束 */ | 601 | /** 公共模块获取token结束 */ |
620 | 602 | ||
@@ -636,11 +618,9 @@ class Almighty extends Api | @@ -636,11 +618,9 @@ class Almighty extends Api | ||
636 | $json_array = json_encode($params); | 618 | $json_array = json_encode($params); |
637 | $headers[] = "Content-Length: ".strlen($json_array); | 619 | $headers[] = "Content-Length: ".strlen($json_array); |
638 | $headers[] = 'Content-Type: application/json; charset=utf-8'; | 620 | $headers[] = 'Content-Type: application/json; charset=utf-8'; |
639 | - | ||
640 | /** 拼接参数结束 **/ | 621 | /** 拼接参数结束 **/ |
641 | 622 | ||
642 | /** asr 请求开始 **/ | 623 | /** asr 请求开始 **/ |
643 | - | ||
644 | $ch = curl_init(); | 624 | $ch = curl_init(); |
645 | curl_setopt($ch, CURLOPT_URL, $ASR_URL); | 625 | curl_setopt($ch, CURLOPT_URL, $ASR_URL); |
646 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | 626 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
@@ -656,7 +636,8 @@ class Almighty extends Api | @@ -656,7 +636,8 @@ class Almighty extends Api | ||
656 | $this->error(curl_error($ch)); | 636 | $this->error(curl_error($ch)); |
657 | } | 637 | } |
658 | curl_close($ch); | 638 | curl_close($ch); |
659 | - | 639 | + /** asr 请求结束 **/ |
640 | + | ||
660 | // 解析结果 | 641 | // 解析结果 |
661 | $response = json_decode($res, true); | 642 | $response = json_decode($res, true); |
662 | 643 |
-
请 注册 或 登录 后发表评论