失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > tp中如何调用科大讯飞的语音转写

tp中如何调用科大讯飞的语音转写

时间:2021-03-31 16:32:28

相关推荐

tp中如何调用科大讯飞的语音转写

1.首先注册科大讯飞(/),拿到语音转写的APPID以及SecretKey

2.将我的压缩包放到tp的扩展类库目录(extend)中。解压

3.在控制里面的调用方式

重点注意:

1.一定要是科大讯飞语音转写的APPID以及SecretKey

2.你的音频文件一定要是绝对路径,这里科大讯飞那边需要将你的文件上传到他们的服务端

3.建议调用的方式采用消息队列的方式,方式因回调慢,影响项目的浏览速度

使用非常简单如有问题联系qq:1219481875

代码

class Kdxf{private $lfasr_host = '/api';# 请求的接口名private $api_prepare = '/prepare';private $api_upload = '/upload';private $api_merge = '/merge';private $api_get_progress = '/getProgress';private $api_get_result = '/getResult';# 文件分片大小10Mprivate $file_piece_sice = 10485760;# 转写类型private $lfasr_type = 0;# 是否开启分词private $has_participle = 'false';private $has_seperate = 'true';# 多候选词个数private $max_alternatives = 0;# 子用户标识private $suid = '';private $appid;private $secret_key;private $upload_file_path;private $obj;private $data;private $sig;public function __construct($appid,$secret_key) {$this->appid = $appid;$this->secret_key = $secret_key;$this->sig = new Sliceidgenerator();}#主要运行函数public function run($file_path) {$this->upload_file_path = $file_path;if($this->upload_file_path!=''){#预处理$pre_result = json_decode($this->prepare_request(),true);//var_dump($pre_result);die();$taskid = $pre_result['data'];#上传文件$this->upload_request($taskid,$this->upload_file_path);$this->merge_request($taskid);while (true) {$progress = $this->get_progress_request($taskid);$progress_dic = json_decode($progress);if ($progress_dic->err_no != 0 && $progress_dic->err_no != 26605){return;}else{$data = $progress_dic->data;$task_status = json_decode($data);if ($task_status->status == 9){//echo('The task ' . $taskid . ' is in processing, task status: ' . $task_status->desc);//echo "<br />";break;}//echo('The task ' . $taskid . ' is in processing, task status: ' . $task_status->desc);//echo "<br />";//sleep(0.1);}}$data_all=$this->get_result_request($taskid=$taskid);$data_all = json_decode($data_all)->data;$data_all=json_decode($data_all);$message='';for ($i=0;$i<count($data_all);$i++){$message.=$data_all[$i]->onebest;} $this->data=$message;return $this->data;}}public function get_data() {return $this->data;}public function gene_params($apiname,$taskid="",$slice_id=""){$appid = $this->appid;$secret_key = $this->secret_key;$upload_file_path = $this->upload_file_path;$ts=time();$md5 = md5($appid.$ts);$signa = hash_hmac('sha1',$md5,$secret_key,true);$signa=base64_encode($signa);$file_len = filesize($this->upload_file_path);$file_name = basename($this->upload_file_path);if($apiname== $this->api_prepare){$slice_num = 1;$param_dict['app_id'] = $appid;$param_dict['signa'] = $signa;$param_dict['ts'] = $ts;$param_dict['file_len'] = (string)($file_len);$param_dict['file_name'] = $file_name;$param_dict['slice_num'] = (string)($slice_num);}elseif($apiname== $this->api_upload){$param_dict['app_id'] = $appid;$param_dict['signa'] = $signa;$param_dict['ts'] = $ts;$param_dict['task_id'] = $taskid;$param_dict['slice_id'] = $slice_id;}elseif($apiname == $this->api_merge){$param_dict['app_id'] = $appid;$param_dict['signa'] = $signa;$param_dict['ts'] = $ts;$param_dict['task_id'] = $taskid;$param_dict['file_name'] = $file_name;}elseif($apiname == $this->api_get_progress || $apiname == $this->api_get_result){$param_dict['app_id'] = $appid;$param_dict['signa'] = $signa;$param_dict['ts'] = $ts;$param_dict['task_id'] = $taskid;}return $param_dict;}public function gene_request($apiname, $data, $files='', $headers=''){if($files!=''){$data['content'] =$files['content'];}$jsonStr =$data;if($apiname==$this->api_prepare||$apiname==$this->api_merge||$apiname==$this->api_get_progress||$apiname==$this->api_get_result){$header= array("Content-Type"=>"application/x-www-form-urlencoded;charset=UTF-8",);}if($apiname==$this->api_upload){$header = array("Content-Type"=>"multipart/form-data;",);}$url = $this->lfasr_host . $apiname;$rt=$this->http_post_json($url, $jsonStr, $header);$result = json_decode($rt[1],true);if ($result["ok"] == 0){//echo("success:".$apiname);//echo "<br />";return $rt[1];}else{return $rt[1];} }# 预处理gene_requestpublic function prepare_request(){return $this->gene_request($apiname=$this->api_prepare,$data=$this->gene_params($this->api_prepare));}#上传public function upload_request($taskid, $upload_file_path){$finfo = finfo_open(FILEINFO_MIME_TYPE);$mime = finfo_file($finfo, $upload_file_path);finfo_close($finfo);$curlFile = curl_file_create($upload_file_path,$mime,pathinfo($upload_file_path,PATHINFO_BASENAME));$files["filename"] = $this->gene_params($this->api_upload)["slice_id"];$files["content"] = $curlFile;$response = $this->gene_request($this->api_upload,$data=$this->gene_params($this->api_upload, $taskid=$taskid,$slice_id=$this->sig->getNextSliceId()),$files=$files);return $response;}public function merge_request($taskid){return $this->gene_request($this->api_merge, $data=$this->gene_params($this->api_merge, $taskid=$taskid));}# 获取进度public function get_progress_request($taskid){return $this->gene_request($this->api_get_progress, $data=$this->gene_params($this->api_get_progress, $taskid=$taskid));}# 获取结果public function get_result_request($taskid){$result=$this->gene_request($this->api_get_result, $data=$this->gene_params($this->api_get_result, $taskid=$taskid));return $result;}#请求public function http_post_json($url, $jsonStr,$header){$ch = curl_init();curl_setopt($ch, CURLOPT_POST, true);curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HTTPHEADER, $header);$response = curl_exec($ch);$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);curl_close($ch);return array($httpCode, $response);}}

如果觉得《tp中如何调用科大讯飞的语音转写》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。