讯飞语音转写php版demo

是转写,不是听写!!!

是转写,不是听写!!!

是转写,不是听写!!!

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

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

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

 

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!!!

你可能感兴趣的:(php,讯飞,语音转写,语音翻译,php,科大讯飞)