public static function curlrequest($url,$data,$method='POST',$useCookie=1) {
// $header[]= 'Content-type:application/json;';
$header[]= "X-HTTP-Method-Override: $method";
/*
if( Request::header("Cookie") && $useCookie) {
$header[] = 'Cookie: sid='.Cookie::get("sid");
}
*/
$ch = curl_init(); //初始化CURL句柄
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch, CURLOPT_HEADER, true);
if (isset($_SERVER['HTTP_USER_AGENT'])) {
curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
} else {
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0');
}
curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式
curl_setopt($ch, CURLOPT_HTTPHEADER, $header );
curl_setopt($ch, CURLOPT_TIMEOUT,30);
if (!empty($data)) {
//$data = json_encode($data);
//echo $data;
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
}
$result = curl_exec($ch);//执行预定义的CURL
$returnData = [];
if(!curl_errno($ch)){
$httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($result, $headerSize);
$header = substr($result,0,$headerSize);
$returnData['body'] = $body;
$returnData['header'] = $header;
$returnData['code'] = $httpcode;
} else {
$returnData['code'] = 500;
}
curl_close($ch);
return $returnData;
}
17:25:56