<?php
namespace App\Http\Controllers\Xfyun;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class SpeedTranscriptionController extends Controller
{
private $upload_file_path;
private $app_id = "";
private $request_id = "";
private $date = "";
private $api_secret = "";
private $api_key = "";
/**
* @return void
*/
public function __construct(Request $request)
{
parent::__construct($request);
$this->app_id = "0ceba603";
$this->request_id = date("YmdHis").rand(10000,99999);
$this->date = gmdate('D, d M Y H:i:s') . ' GMT';
$this->api_secret = "";
$this->api_key = "";
}
/**
* 获得任务结果
*/
public function pro_get(){
$host = "ost-api.xfyun.cn";
$query = "/v2/ost/query";
$url = "https://{$host}{$query}";
$task_id = "230403162750667586272853";
$body = [
"common"=> [
"app_id" => $this->app_id
],
"business"=> [
"task_id"=> $task_id,
]
];
$body = json_encode($body);
$header = $this->getHeader($host,$query);
$r = $this->curl_request($url,'POST',$body,$header);
var_dump($r);exit;
}
/**
* 签名
* 得到header
*/
public function getHeader($host,$query,$content_Type = "application/json",$method = "POST"){
$digest = "SHA-256=".base64_encode(hash("sha256","", true));
$signature = "host: ".$host."\n"."date: ".$this->date."\n{$method} {$query} HTTP/1.1\n"."digest: ".$digest;
$signature = base64_encode(hash_hmac('sha256', $signature, $this->api_secret,true));
$authorization = 'api_key="'.$this->api_key.'",algorithm="hmac-sha256",headers="host date request-line digest",signature="'.$signature.'"';
$header = array(
"host:"." ".$host,
"date:"." ".$this->date,
"authorization:"." ".$authorization,
"digest:"." ".$digest,
"content-Type: ".$content_Type,
);
return $header;
}
/**
* 创建任务
*/
public function pro_create(){
$host = "ost-api.xfyun.cn";
$query = "/v2/ost/pro_create";
$url = "https://{$host}{$query}";
$aurl= "https://xfyun-seve-dx/";//链接-科大讯飞上传后返回的
$body = [
"common"=> [
"app_id" => $this->app_id
],
"business"=> [
"request_id"=> $this->request_id,
"language"=> "zh_cn",
"domain"=> "pro_ost_ed",
"accent"=> "mandarin",
//"callback_url"=> "your_callback_url" //可选字段
],
"data"=> [
"audio_url"=> $aurl,
"audio_src"=> "http",
"format"=>"audio/L16;rate=16000",
"encoding"=>"raw"
]
];
$body = json_encode($body);
$header = $this->getHeader($host,$query);
$r = $this->curl_request($url,'POST',$body,$header);
var_dump($r);exit;
}
/**
* 小文件上传
*/
public function upload(){
$this->upload_file_path = "./uploads/20230403/2.wav";
$host = "upload-ost-api.xfyun.cn";
$query = "/file/upload";
$url = "https://".$host.$query;
$body = [
'request_id' => $this->request_id,
'app_id' => $this->app_id,
'data' => $this->upload_request($this->upload_file_path)
];
$header = $this->getHeader($host,$query,"multipart/form-data");
$r = $this->curl_request($url,'POST',$body,$header);
var_dump($r);exit;
}
/**
* 文件file
*/
public function upload_request($upload_file_path){
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $upload_file_path);
finfo_close($finfo);
$curlFile = curl_file_create(
$upload_file_path,
$mime,
pathinfo(
$upload_file_path,
PATHINFO_BASENAME
)
);
return $curlFile;
}
/**
* CURL请求
* @param string $url
* @param string $method
* @param array $params
* @param array $headers
*/
public function curl_request($url = '',$method = 'POST',$params = array(),$headers = array()){
$method = strtoupper($method);
//初始化
$ch = curl_init();
// 在HTTP请求头中"Referer: "的内容。
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //验证curl对等证书(一般只要此项)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //检查服务器SSL证书中是否存在一个公用名
curl_setopt($ch, CURLOPT_SSLVERSION, 0); //传递一个包含SSL版本的长参数。
if($method == 'GET' && !empty($params) && is_array($params)){
$url .= '?'.http_build_query($params);
}else if ($method == 'POST') {
//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}else if ($method == 'PUT') {
//启用时允许HTTP发送文件,必须同时设置CURLOPT_INFILE和CURLOPT_INFILESIZE。
curl_setopt($ch, CURLOPT_PUT, true);
// if($this->upload_file_path){
// curl_setopt($ch, CURLOPT_INFILE, fopen($this->upload_file_path));
// curl_setopt($ch, CURLOPT_INFILESIZE, filesize($this->upload_file_path));
// }
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
//CURLOPT_SSL_VERIFYPEER(默认值为2)被启用,CURLOPT_SSL_VERIFYHOST需要被设置成TRUE否则设置为FALSE。
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
//返回的内容作为变量储存,而不是直接输出。必需设置curl的CURLOPT_RETURNTRANSFER选项为1或true。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//设置为0表示不返回HTTP头部信息
curl_setopt($ch, CURLOPT_HEADER, false);
//在发起连接前等待的时间,如果设置为0,则无限等待
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
if(!empty($headers)){
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
//启用时会将服务器服务器返回的"Location: "放在header中递归的返回给服务器,使用CURLOPT_MAXREDIRS可以限定递归返回的数量。
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
//设置为1表示稍后执行的curl_exec函数的返回是URL的返回字符串,而不是把返回字符串定向到标准输出并返回TRUE
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//执行并获取结果
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$err = curl_error($ch);
//释放curl句柄
curl_close($ch);
return $result;
}
}
Démonstration de transcription vocale extrêmement rapide de Keda Xunfei (PHP-laravel)
Guess you like
Origin blog.csdn.net/qq960685827/article/details/129935141
Ranking