//自定义通用curl操作
public static function makeCurl($url,$request='',$user_agent='',$headers='',$only_headers=''){
$ch = curl_init();//初始化
curl_setopt($ch,CURLOPT_URL,$url);//指定访问的url
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,30);//超时时间
//跳过https验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//判断是否使用代理
if($user_agent){
curl_setopt($ch,CURLOPT_USERAGENT,$user_agent);
}
//判断是否post请求
if($request){
curl_setopt($ch, CURLOPT_POST, 1);//采用post方式请求
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//设定返回的数据是否自动显示
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);//post请求数据 , 这个要根据 header头content-type:(数据类型格式) 去定义,比如json的就用json_encode()格式再传;urlencode的就用http_build_query()格式一下
}
if($headers){
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//header头格式 一定要按照
}
//只返回header头的location
if($only_headers){
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
}
$c = curl_exec($ch);
if($only_headers){
$c = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL);//获取response回来的header头数据
}
curl_close($ch);
return $c;
}
//headers 头格式一定要按照下面的传值,否则会爆炸!不能随便来啊
$headers = [
'Accept:application/json',
'Content-Type:application/json;charset="utf-8"',
'Content-Length:'.strlen($request_json),
'Authorization:'.$authorization,
];