方法:
function curlRequest($url,$method='',$header=[],$params=[]){
$curl = curl_init(); //初始化
curl_setopt($curl, CURLOPT_URL, $url); //设置抓取的url
curl_setopt($curl, CURLOPT_HEADER, 0); //设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
if($method !=''){
$method = strtolower($method);
switch($method) {
case 'post':
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params); //设置请求体,提交数据包
break;
case 'put':
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_POSTFIELDS, $params); //设置请求体,提交数据包
break;
case 'delete':
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
break;
}
}
//执行命令
$data = curl_exec($curl);