讯飞语音转文字PHP tp6 demo
讯飞官网没有PHP demo我是很诧异的
我php天下第一就这么没牌面吗
网上找了很久没找到 tp的demo,借鉴了
一位老哥的yii框架 demo
改成了我需要的tp6 demo
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';
const UPLOAD_URL = 'http://raasr.xfyun.cn/api/upload';
const MERGE_URL = 'http://raasr.xfyun.cn/api/merge';
const GET_PROGRESS_URL = 'http://raasr.xfyun.cn/api/getProgress';
const GET_RESULT_URL = 'http://raasr.xfyun.cn/api/getResult';
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]);
}
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;
}
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;
}
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;
}
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;
}
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;
}
public static function curlPost($url, $postData = '', $header = '')
{
$curl = curl_init();
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);
curl_close($curl);
return $response;
}
public static function getSinga($appId, $secretKey, $ts)
{
$md5Str = $appId . $ts;
$md5 = MD5($md5Str);
$md5 = mb_convert_encoding($md5, "UTF-8");
$signa = base64_encode(hash_hmac("sha1", $md5, $secretKey, true));
return $signa;
}
}
githup地址 https://github.com/xy6409068/xunfei_tp6
Original: https://blog.csdn.net/qq_49504079/article/details/109889841
Author: 安徽许彦祖
Title: 讯飞语音转文字 PHP demo
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/526488/
转载文章受原作者版权保护。转载请注明原作者出处!