/*
**PHP 构造 模拟 http 请求 https_request 支持GET和POST
*/
function https_request($url, $data = null)
{
$curl = curl_init();//初始化
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);//允许 cURL 函数执行的最长秒数。
/*if (!empty($port)) {
curl_setopt($curl, CURLOPT_PORT, $port);//可选的用来指定连接端口,默认80端口可不写
}*/
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json;charset=UTF-8'));//设置请求目标url头部信息
if (!empty($data)) {
//$data不为空,发送post请求
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); //$data:数组
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);//执行命令
$error = curl_error($curl);//错误信息
if ($error || $output == FALSE) {
//报错信息
return 'ERROR ' . curl_error($curl);
}
curl_close($curl);
return $output;
}
PHP 构造模拟http请求https_request支持GET和POST
2022.09.13
暂无评论 5688个游客飘过
相关文章
分享