/**
* 简单通用请求
* @params [
* 'url'=>'', : wwww.x
* 'type'=>'', : POST | GET
* 'data'=>'', : [key=>'val'] | 'para1=val1¶2=val2'
* 'headers'=> '' : ['cookie:xxxx']
* ]
*/
function sendRequest($params){
if(!isset($params['url'])){
throw ('url cannot be empty!');
exit;
}
$_params = [
'url'=>$params['url'],
'type'=>isset($params['type'])?$params['type']:'GET',
'data'=>isset($params['data'])?$params['data']:'',
'headers'=>isset($params['headers'])?$params['headers']:[],
];
$ch = curl_init($_params['url']);
if(strtoupper($_params['type'])=="POST"){
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_params['data']);
}else{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $_params['type']);
}
curl_setopt($ci, CURLOPT_HTTPHEADER, $_params['headers']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER ,false);
curl_setopt($ch, CURLOPT_HEADER, true);//需要response header 则开启
$respones = curl_exec($ch);
$info = curl_getinfo($ch);
$resheader = substr($respones, 0,$info['header_size']);
$resbody = substr($respones, $info['header_size']);
return ['info'=>$info,'header'=>$resheader,'body'=>$resbody];
}
网友评论