<?php
namespace App\Http\Controllers\Xfyun;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PhoneticTranscriptionController extends Controller
{
private $appid;
private $secret_key;
private $upload_file_path;
private $ts;
public function __construct(Request $request){
parent::__construct($request);
$this->upload_file_path = "";
$this->appid = "";
$this->secret_key = "";
$this->ts = time();
}
/**
* 得到结果
*/
public function getResult(){
$url = "http://raasr.xfyun.cn/v2/api/getResult";
$orderId = "DKHJQ20230403112232432AkEdERDtiZuYgI7t";
$signa = $this->getSigna();
$url .= "?signa={$signa}&orderId=".$orderId."&resultType=transfer&appId=".$this->appid."&ts=".$this->ts;
$header = array("Content-Type: application/json; charset=UTF-8");
$re = $this->curl_request($url,"POST","",$header);
var_dump($re);exit;
}
public function upload() {
if($this->upload_file_path!=''){
$url = "https://raasr.xfyun.cn/v2/api/upload";
$filesize = filesize($this->upload_file_path);
$signa = $this->getSigna();
$url .= "?duration=2&signa=".$signa."&fileName=".(basename($this->upload_file_path))."&fileSize=".$filesize."&sysDicts=uncivilizedLanguage&appId=".$this->appid."&ts=".$this->ts;
$fp = fopen($this->upload_file_path,'r');
$body = fread($fp,$filesize);
fclose($fp);
$header = array("Content-Type: application/json; charset=UTF-8,Chunked: false");
$re = $this->curl_request($url,"POST",$body,$header);
var_dump($re);exit;
}
}
/**
* 签名
*/
public function getSigna(){
$baseString = md5($this->appid.$this->ts);
$signa = hash_hmac('sha1',$baseString,$this->secret_key,true);
$signa=base64_encode($signa);
return $signa;
}
/**
* 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);
}
//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;
}
}
科大讯飞 语音转写 demo (PHP-Laravel)
最新推荐文章于 2024-06-25 19:30:12 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)