是转写,不是听写!!!

是转写,不是听写!!!

是转写,不是听写!!!

讯飞开放平台提供的api里面,demo只提供了py3和java的版本!!!

github包括码云我也没有找到现成的代码(不一定没有,或许是我没有找到),只能自己写一份!!!其实是很简单的几个api,但是自己写一遍还会发现里面的坑,比如文档中的部分参数是string类型的bool值,吐槽一下!!!

我们用的是Yii2,所以demo也是Yii2的风格,代码是需要根据自己的需求改动的,比如分片,我们的场景是不需要的,所以是直接写死的,下面是我的demo,希望能帮到部分有需要的人:

<?phpnamespace frontend\controllers;use Yii;class AudioController extends BaseController
{const PREPARE_URL       = 'http://raasr.xfyun.cn/api/prepare';// 预处理 /prepare:const UPLOAD_URL        = 'http://raasr.xfyun.cn/api/upload';// 文件分片上传 /upload:const MERGE_URL         = 'http://raasr.xfyun.cn/api/merge';// 合并文件 /merge:const GET_PROGRESS_URL  = 'http://raasr.xfyun.cn/api/getProgress';// 查询处理进度 /getProgress:const GET_RESULT_URL    = 'http://raasr.xfyun.cn/api/getResult';// 获取结果 /getResult:/*** 翻译接口** @return array* @throws RunException*/public function actionExec(){$result = $this->requestData;// 接收参数if (empty($result->file)) {throw new RunException('请求参数异常', 400);}$mime = (new \finfo(FILEINFO_MIME_TYPE))->file($result->file);$file = $result->file;$appId = Yii::$app->params['xunFeiAppId'];$secretKey = Yii::$app->params['xunFeiSecretKey'];$prepareData = static::prepare($appId, $secretKey, $file);if ($prepareData['ok'] != 0) {throw new RunException('prepare失败');}$taskId = $prepareData['data'];$uploadData = static::upload($appId, $secretKey, $file, $mime, $taskId);if ($uploadData['ok'] != 0) {throw new RunException('upload失败');}$mergeData = static::merge($appId, $secretKey, $taskId);if ($mergeData['ok'] != 0) {throw new RunException('merge失败');}$num = 1;start:$getProgressData = static::getProgress($appId, $secretKey, $taskId);if ($getProgressData['ok'] != 0) {throw new RunException('getProgress失败');}$statusData = json_decode($getProgressData['data'], true);if ($statusData['status'] != 9) {if ($num >= 10) {throw new RunException('转写时间过长');}$num++;sleep(1);goto start;}$getResultData = static::getResult($appId, $secretKey, $taskId);if ($getResultData['ok'] != 0) {throw new RunException('getResult失败');}return $this->returnJsonData($getResultData['data']);}/*** 预处理** @param $appId* @param $secretKey* @param $file* @return mixed*/public static function prepare($appId, $secretKey, $file){$fileInfo = pathinfo($file);$ts = time();$data = ['app_id' => (string)$appId,'signa' => (string)static::getSinga($appId, $secretKey, $ts),'ts' => (string)$ts,'file_len' => (string)filesize($file),'file_name' => (string)$fileInfo['basename'],'slice_num' => 1,'has_participle' => (string)"true",//转写结果是否包含分词信息];$data = http_build_query($data);$header = ['Content-Type: application/x-www-form-urlencoded; charset=UTF-8'];$res = static::curlPost(static::PREPARE_URL, $data, $header);$resultData = json_decode($res, true);return $resultData;}/*** 上传文件** @param $appId* @param $secretKey* @param $file* @param $taskId* @return mixed*/public static function upload($appId, $secretKey, $file, $mime, $taskId){$ts = time();$curlFile = curl_file_create($file,$mime,pathinfo($file,PATHINFO_BASENAME));$data = ['app_id' => (string)$appId,'signa' => (string)static::getSinga($appId, $secretKey, $ts),'ts' => (string)$ts,'task_id' => $taskId,'slice_id' => "aaaaaaaaaa",'content' => $curlFile,];$header = ["Content-Type: multipart/form-data"];$res = static::curlPost(static::UPLOAD_URL, $data, $header);$resultData = json_decode($res, true);return $resultData;}/*** 合并文件** @param $appId* @param $secretKey* @param $taskId* @return mixed*/public static function merge($appId, $secretKey, $taskId){$ts = time();$data = ['app_id' => (string)$appId,'signa' => (string)static::getSinga($appId, $secretKey, $ts),'ts' => (string)$ts,'task_id' => $taskId,];$data = http_build_query($data);$header = ["Content-Type: application/x-www-form-urlencoded; charset=UTF-8"];$res = static::curlPost(static::MERGE_URL, $data, $header);$resultData = json_decode($res, true);return $resultData;}/*** 查询处理进度** @param $appId* @param $secretKey* @param $taskId* @return mixed*/public static function getProgress($appId, $secretKey, $taskId){$ts = time();$data = ['app_id' => (string)$appId,'signa' => (string)static::getSinga($appId, $secretKey, $ts),'ts' => (string)$ts,'task_id' => $taskId,];$data = http_build_query($data);$header = ["Content-Type: application/x-www-form-urlencoded; charset=UTF-8"];$res = static::curlPost(static::GET_PROGRESS_URL, $data, $header);$resultData = json_decode($res, true);return $resultData;}/*** 获取转写结果** @param $appId* @param $secretKey* @param $taskId* @return mixed*/public static function getResult($appId, $secretKey, $taskId){$ts = time();$data = ['app_id' => (string)$appId,'signa' => (string)static::getSinga($appId, $secretKey, $ts),'ts' => (string)$ts,'task_id' => $taskId,];$data = http_build_query($data);$header = ["Content-Type: application/x-www-form-urlencoded; charset=UTF-8"];$res = static::curlPost(static::GET_RESULT_URL, $data, $header);$resultData = json_decode($res, true);return $resultData;}/*** curl** @param $url* @param string $postData* @param string $header* @return bool|string*/public static function curlPost($url, $postData = '', $header = ''){//初始化$curl = curl_init(); //用curl发送数据给apicurl_setopt($curl, CURLOPT_POST, true);curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);curl_setopt($curl, CURLOPT_URL, $url);if (!empty($header)) {curl_setopt($curl, CURLOPT_HTTPHEADER, $header);}if (!empty($postData)) {curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);}curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);$response = curl_exec($curl);//关闭URL请求curl_close($curl);//显示获得的数据return $response;}/*** 获取signa** @param $appId* @param $secretKey* @param $ts* @return string*/public static function getSinga($appId, $secretKey, $ts){$md5Str = $appId . $ts;$md5 = MD5($md5Str);$md5 = mb_convert_encoding($md5, "UTF-8");// 相当于java的HmacSHA1方法$signa = base64_encode(hash_hmac("sha1", $md5, $secretKey, true));return $signa;}}

有错误也请评论指出,我会及时修改!!!

欢迎转载,注明出处:https://blog.csdn.net/wb_json/article/details/95050959!!!

讯飞语音转写php版demo相关推荐

  1. AI赋能日常生活:讯飞语音转写在会议、采编、上课等场景中的应用

    人工智能在日常生活中的应用已经非常广泛了.除了ChatGPT这种对话生成式AI,语音转文字技术也可以在很多场景中派上用场.比如,奔波于各种会议之间的打工人.经常采访的媒体从业者以及要上网课的学生们,每 ...

  2. html5语音听写流式,iOS 讯飞语音听写(流式版)

    最近项目中用到了讯飞的语音识别,然后稍微看了一下,里面有几个值得注意的点,记录一下,先说语音听写(流式版),实时语音转写后期会附上 ,文末有 demo //语音听写(流式版) 语音听写流式版其实没设么 ...

  3. 讯飞语音转写.NET版本

    吐槽一下,讯飞官方webapi,没有提供.NET版本案例,只有python,java,只好自己摸索,代码不太简洁,这里复习总结一下,权当作做笔记摘抄一样总结一番,也给第一天尝试其他小伙伴一点参考. 检 ...

  4. 讯飞语音转文字 PHP demo

    讯飞语音转文字PHP tp6 demo 讯飞官网没有PHP demo我是很诧异的 改成了我需要的tp6 demo 讯飞官网没有PHP demo我是很诧异的 我php天下第一就这么没牌面吗 网上找了很久 ...

  5. Python讯飞语音转文字保存到文件

    因为有朋友需要将录音转成文字, 给我的是m4a格式, 我给转成txt发给他的. 我找了找, 发现网上很多都是收费软件,而且转换结果不尽人意., 最后决定使用讯飞服务来完成转换, 讯飞语音转写api支持 ...

  6. Android 文字转语音使用讯飞语音SDK(eclipse版 无UI)

    Android 文字转语音使用讯飞语音SDK(eclipse版) 1.下载SDK(地址:http://www.xfyun.cn/sdk/dispatcher)下载前会让你先创建应用,创建应用后会得到一 ...

  7. 讯飞语音--唤醒Demo

    写的第一篇博客,因为最近姐姐说起了一个段子, 一男子在地铁站手机找不到了,但是带了蓝牙耳机,耳机还有内容,男子想手机一定还在附近,随即大喊一句,悟空你在哪儿, 手机循环回答,我在这.....这时,拿手 ...

  8. 前端之实现讯飞语音听写(流式版)

    第一次接到语音需求,用了几年的微信,你有关注过微信语音实时转文字功能吗?反正我是经常用,在这人山人海的大城市,为了解放双手又能及时回复好友,我经常用语音转文字. 没想到,一时用一时爽,自己开发火葬场. ...

  9. 讯飞社区android 源码,android 讯飞语音 demo

    [实例简介] android 讯飞语音 demo 博客地址:http://blog.csdn.net/chenshufei2/article/details/8496905 [实例截图] [核心代码] ...

最新文章

  1. C++逆流而上,Java 惨不忍睹 | 9月编程语言排行
  2. 以后教育孩子学好数学的方法 多思动漫数学
  3. 聊一聊Java中的文件锁
  4. Python 多进程 multiprocessing 使用示例
  5. ec200t 拨号_移远EC20 R2.0 AT指令拨号流程
  6. 翻译:如何在Mac OS X中设置文件权限chmod
  7. 【项目实战】Python基于Apriori关联规则算法实现商品零售购物篮分析
  8. 小米汽车VS苹果汽车,相同赛道不同逻辑
  9. 无线音箱解决方案开发过程
  10. R绘图笔记 | 生存曲线的绘制
  11. 【计算机视觉】深度相机(八)--OpenNI及与Kinect for windows SDK的比较
  12. android 键盘 定义,自定义全键盘-[Android_YangKe]
  13. jenkins API 使用postman调用
  14. CSU1041——单词统计
  15. WebDAV之葫芦儿·派盘+FX文件管理器
  16. 大数据开发和java开发到底有什么不同?
  17. WebRequest 和 HttpWebRequest 区别
  18. 迁移gogs直接拷贝其gogs-repositories导致hook脚本中的相关路径与实际不相符
  19. 无线路由器当作无线交换机使用
  20. 51单片机学习笔记——蜂鸣器

热门文章

  1. 微软面试题之数字谜题
  2. web前端开发常用的10个高端CSS UI开源框架
  3. UWB超宽带 DW1000 通道和带宽
  4. Re:从零开始的DS学习之查找算法
  5. python3计算md5_python 计算文件的md5值实例
  6. php中访问excel文件,PHP中常用的Excel文件访问类及修改 | 学步园
  7. 限制输入框输入(数字、小数点、负号)
  8. 云计算与大数据平台课堂作业
  9. Win10怎么删除快速访问中最近使用文件记录
  10. R 和 Rstudio 在线更新