讯飞语音转写php版demo

是转写,不是听写!!!

是转写,不是听写!!!

是转写,不是听写!!!

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

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

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

 

<?php

namespace 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发送数据给api
        curl_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!!!

  • 2
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论 8

打赏作者

wb_json

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值