PHP发起post请求
xuexi 2024-01-04 09:43:59 发表在:PHP 查看数:1041

PHP发起post请求

<?php
define('DEMO_CURL_VERBOSE', true); // 打印curl debug信息

/**
* php -m 检查是否开启php curl扩展
*/

# 填写网页上申请的appkey 如 $apiKey="g8eBUMSokVB1BHGmgxxxxxx"
$API_KEY =  "E4G8LDuAzIGiOYc6q7qtWmOG";

# 填写网页上申请的APP SECRET 如 $secretKey="94dc99566550d87f8fa8ece112xxxxx"
$SECRET_KEY = "8G2uP9SZI6WAgeC6GNWI6pH84niHnYro";

# 需要识别的文件
//$AUDIO_FILE = "http://162.14.74.173/1.m4a";
# 文件格式
$API='24.83baa0e25f59dcfc7114b2df37ad143e.2592000.1706863831.282335-45498439';



/** 拼接参数开始 **/
// $audio = file_get_contents($AUDIO_FILE);
// $base_data = base64_encode($audio);
$params = array(

 //"task_ids" => ["659523e2fe472e000127b0eb"],
 "task_ids" => ["65952594fe472e000127b175"],

);

 $json_array = json_encode($params);
// $headers[] = "Content-Length: ".strlen($json_array);
$headers[] = 'Content-Type: application/json; charset=utf-8';

/** 拼接参数结束 **/

/** asr 请求开始 **/
$ASR_URL = "https://aip.baidubce.com/rpc/2.0/aasr/v1/query?access_token=$API";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ASR_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 6000); // 识别时长不超过原始音频
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_array);
curl_setopt($ch, CURLOPT_VERBOSE, DEMO_CURL_VERBOSE);
$res = curl_exec($ch);
if(curl_errno($ch))
{
    echo curl_error($ch);
    exit (2);
}
curl_close($ch);
/** asr 请求结束 **/

// 打印请求参数
//echo 'url is : ' . $ASR_URL . "\n";
// $params['speech'] = 'base64_encode(file_get_contents($AUDIO_FILE))'; 
//echo 'params is :' . json_encode($params) . "\n";

// 打印百度返回的结果
//echo "asr result $res\n";

// 解析结果
$response = json_decode($res, true);

if (isset($response['err_no']) && $response['err_no'] == 0){
    echo "asr result is ". $response['result'][0] . "\n";
}else{
    echo "asr has error\n";
}

var_dump($response);

// windows命令行遇见乱码可打开文件result.txt, 使用如notepad++等软件打开
// echo "response is also saved into result.txt in UTF-8\n ";
// file_put_contents("result.txt", $res);
?>
最近访问时间:2025-04-29 19:29:03
知识库:340条鸣谢:TAY  备案号:蜀ICP备2024090044号