/**
* [request PHP CURL 操作]
* @author Williamslife
* Email 876743701@qq.com
* @DateTime 2019-11-26T11:54:39+0800
* @param [type] $url [description]
* @param boolean $https [description]
* @param string $method [description]
* @param [array or json depends on the other side's require] $data [description]
* @param [array] $header [description]
* @return [type] [description]
*/
public function request($url, $https=true, $method="get", $data=null, $header=array())
{
// 1.初始化url
$ch = curl_init($url);
// 2.设置相关参数 字符串不能直接输出,进行一个变量的存储
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_HEADER, 1);
// 判断是不是https请求
if ($https === true) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
// 判断是否为post请求
if ($method == 'post') {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
//设置header头
//传过来的header头应该是这样的
//$headers = array(
//"Content-Type:application/json",
//"Content-Length:" . strlen($trans),
//"Accept:Content-Type:application/json;charset=utf-8",
// "client-id:5K8264ILTKCH16CQ2502SI8ZNMTM67VS", //自定义参数
// "client-secret:C380BEC2BFD727A4B6845133519F3AD6" //自定义参数
//);
if (!empty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
// 3.发送请求
$str = curl_exec($ch);
// $hd = curl_getinfo($ch);
// 4.关闭连接
curl_close($ch);
// 返回请求到的结果
// return array("str" => $str, "hd" => $hd);
return $str;
}
PHP CURL 操作
最新推荐文章于 2022-06-28 18:16:44 发布