讯飞语音转文字 PHP demo

讯飞官网没有PHP demo我是很诧异的

我php天下第一就这么没牌面吗
网上找了很久没找到 tp的demo,借鉴了
一位老哥的yii框架 demo

改成了我需要的tp6 demo

<?php

namespace app\index\controller;

use app\lib\exception\BaseExcption;
use think\Exception;
use think\response\Json;

class XunFei
{
    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:

//      测试表单页面
//    public function index(){
//        return view();
//    }

    /**
     * 翻译接口
     *
     * @return Json
     * @throws BaseExcption
     */
    public function actionExec()
    {
        // 接收参数
        if (empty(request()->file('file'))) {
            throw new Exception('请求参数异常', 400);
        }
        //接收文件
        $file = request()->file('file');
        //获取文件类型信息
        $mime = $file->getOriginalMime();
        //文件上传兵返回文件名称
        $savename = \think\facade\Filesystem::disk('public')->putFile( 'xun_fei', $file);
        if(!$savename){
            throw new Exception('文件上传失败');
        }
        $file =  'storage/' . $savename;
        //讯飞语音转文字密钥
        $appId = env('xunfei.app_id');
        $secretKey = env('xunfei.app_key');

        $prepareData = static::prepare($appId, $secretKey, $file);
        if ($prepareData['ok'] != 0) {
            throw new Exception('prepare失败');
        }

        $taskId = $prepareData['data'];

        $uploadData = static::upload($appId, $secretKey, $file, $mime, $taskId);
        if ($uploadData['ok'] != 0) {
            throw new Exception('upload失败');
        }

        $mergeData = static::merge($appId, $secretKey, $taskId);

        if ($mergeData['ok'] != 0) {
            throw new Exception('merge失败');
        }

        $num = 1;

        //限定转写次数
        start:
        $getProgressData = static::getProgress($appId, $secretKey, $taskId);

        if ($getProgressData['ok'] != 0) {
            throw new Exception('getProgress失败');
        }
        $statusData = json_decode($getProgressData['data'], true);

        if ($statusData['status'] != 9) {
            if ($num >= 10) {
                throw new Exception('转写时间过长');
            }
            $num++;
            sleep(1);
            goto start;
        }

        $getResultData = static::getResult($appId, $secretKey, $taskId);
        if ($getResultData['ok'] != 0) {
            throw new Exception('getResult失败');
        }
        //分词数组
        $data = json_decode($getResultData['data'],true);
        //拼接分词
        $text = '';
        foreach ($data as $v) {
            $text.= $v['onebest'];
        }

        return json(['code'=>0,'msg'=>'ok','data'=>$text]);
    }


    /**
     * 预处理
     *
     * @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)"false",//转写结果是否包含分词信息
        ];

        $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;
    }

}

#githup地址 https://github.com/xy6409068/xunfei_tp6

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安徽许彦祖

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值