??
CURL 发送 GET 请求
function?curl_get_request($url,$data=null){
if(is_array($data)){
$data?=?http_build_query($data);
}
$ch?=?curl_init();
curl_setopt($ch,?CURLOPT_URL,?$url);
curl_setopt($ch,?CURLOPT_HEADER,?false);//不返回头部信息
if($data!=null){
curl_setopt($ch,?CURLOPT_POSTFIELDS,?$data);
}
curl_setopt($ch,?CURLOPT_RETURNTRANSFER,1);??//结果是否显示出来,1不显示,0显示????
//判断是否https
if(strpos($url,'https://')!==false){
curl_setopt($ch,?CURLOPT_SSL_VERIFYPEER,?false);
curl_setopt($ch,?CURLOPT_SSL_VERIFYHOST,?false);
$UserAgent?=?'Mozilla/4.0?(compatible;?MSIE?7.0;?Windows?NT?6.0;?SLCC1;?.NET?CLR?2.0.50727;?.NET?CLR?3.0.04506;?.NET?CLR?3.5.21022;?.NET?CLR?1.0.3705;?.NET?CLR?1.1.4322)';
curl_setopt($ch,?CURLOPT_USERAGENT,?$UserAgent);//设置Agent
}
$data?=?curl_exec($ch);
curl_close($ch);
if($data?===?FALSE)?
{?
??$data?=?"curl?Error:".curl_error($ch);
}?
return?$data;
}
CURL 发送 POST 请求
function?curl_post_request($url,$data=null){
if(is_array($data)){
$data?=?http_build_query($data);
}
$ch?=?curl_init();
curl_setopt($ch,?CURLOPT_URL,?$url);
curl_setopt($ch,?CURLOPT_HEADER,?false);//不返回头部信息
curl_setopt($ch,?CURLOPT_POST,?1);
if($data!=null){
curl_setopt($ch,?CURLOPT_POSTFIELDS,?$data);
}
curl_setopt($ch,?CURLOPT_RETURNTRANSFER,1);??//结果是否显示出来,1不显示,0显示????
//判断是否https
if(strpos($url,'https://')!==false){
curl_setopt($ch,?CURLOPT_SSL_VERIFYPEER,?false);
curl_setopt($ch,?CURLOPT_SSL_VERIFYHOST,?false);
$UserAgent?=?'Mozilla/4.0?(compatible;?MSIE?7.0;?Windows?NT?6.0;?SLCC1;?.NET?CLR?2.0.50727;?.NET?CLR?3.0.04506;?.NET?CLR?3.5.21022;?.NET?CLR?1.0.3705;?.NET?CLR?1.1.4322)';
curl_setopt($ch,?CURLOPT_USERAGENT,?$UserAgent);
}
$data?=?curl_exec($ch);
curl_close($ch);
if($data?===?FALSE)?
{?
??$data?=?"curl?Error:".curl_error($ch);
}?
return?$data;
}
封装GET和POST请求
function?curl_request($url,$data=null,$method="GET"){
if(is_array($data)){
$data?=?http_build_query($data);
}
$ch?=?curl_init();
curl_setopt($ch,?CURLOPT_URL,?$url);
curl_setopt($ch,?CURLOPT_HEADER,?false);//不返回头部信息
if(strtolower($method)!='get'){
curl_setopt($ch,?CURLOPT_POST,?1);
}
if($data!=null){
curl_setopt($ch,?CURLOPT_POSTFIELDS,?$data);
}
curl_setopt($ch,?CURLOPT_RETURNTRANSFER,1);??//结果是否显示出来,1不显示,0显示????
//判断是否https
if(strpos($url,'https://')!==false){
curl_setopt($ch,?CURLOPT_SSL_VERIFYPEER,?false);
curl_setopt($ch,?CURLOPT_SSL_VERIFYHOST,?false);
$UserAgent?=?'Mozilla/4.0?(compatible;?MSIE?7.0;?Windows?NT?6.0;?SLCC1;?.NET?CLR?2.0.50727;?.NET?CLR?3.0.04506;?.NET?CLR?3.5.21022;?.NET?CLR?1.0.3705;?.NET?CLR?1.1.4322)';
curl_setopt($ch,?CURLOPT_USERAGENT,?$UserAgent);
}
$data?=?curl_exec($ch);
curl_close($ch);
if($data?===?FALSE)?
{?
??$data?=?"curl?Error:".curl_error($ch);
}?
return?$data;
}
CURL 使用 COOKIE 实现模拟用户登录爬取信息
//$cookie?cookie文件链接
function?curl_request($url,$data=null,$cookie='',$method='GET'){
if(is_array($data)){
?$data?=?http_build_query($data);
}
$ch?=?curl_init();
curl_setopt($ch,?CURLOPT_URL,?$url);
curl_setopt($ch,?CURLOPT_HEADER,?false);
if($method!='GET'){
curl_setopt($ch,?CURLOPT_POST,?1);
}
if($data!=null){
curl_setopt($ch,?CURLOPT_POSTFIELDS,?$data);
}
curl_setopt($ch,?CURLOPT_RETURNTRANSFER,1);??//结果是否显示出来,1不显示,0显示????
//判断是否https
if(strpos($url,'https://')!==false){
curl_setopt($ch,?CURLOPT_SSL_VERIFYPEER,?false);
curl_setopt($ch,?CURLOPT_SSL_VERIFYHOST,?false);
$UserAgent?=?'Mozilla/4.0?(compatible;?MSIE?7.0;?Windows?NT?6.0;?SLCC1;?.NET?CLR?2.0.50727;?.NET?CLR?3.0.04506;?.NET?CLR?3.5.21022;?.NET?CLR?1.0.3705;?.NET?CLR?1.1.4322)';
curl_setopt($ch,?CURLOPT_USERAGENT,?$UserAgent);
}
$cookie_file?=?file_get_contents($cookie);
curl_setopt($ch,?CURLOPT_COOKIE,?$cookie_file);
$data?=?curl_exec($ch);
if($data?===?FALSE)?
{?
??$data?=?"curl?Error:".curl_error($ch);
}?
curl_close($ch);
return?$data;
}
CURL加请求header参数
function?curl_get_request($url,$data=null,$header=null){
if(is_array($data)){
$data?=?http_build_query($data);
}
$ch?=?curl_init();
curl_setopt($ch,?CURLOPT_URL,?$url);
curl_setopt($ch,?CURLOPT_HEADER,?false);//不返回头部信息
if($data!=null){
curl_setopt($ch,?CURLOPT_POSTFIELDS,?$data);
}
curl_setopt($ch,?CURLOPT_RETURNTRANSFER,1);??//结果是否显示出来,1不显示,0显示????
//判断是否https
if(strpos($url,'https://')!==false){
curl_setopt($ch,?CURLOPT_SSL_VERIFYPEER,?false);
curl_setopt($ch,?CURLOPT_SSL_VERIFYHOST,?false);
$UserAgent?=?'Mozilla/4.0?(compatible;?MSIE?7.0;?Windows?NT?6.0;?SLCC1;?.NET?CLR?2.0.50727;?.NET?CLR?3.0.04506;?.NET?CLR?3.5.21022;?.NET?CLR?1.0.3705;?.NET?CLR?1.1.4322)';
curl_setopt($ch,?CURLOPT_USERAGENT,?$UserAgent);//设置Agent
}
if($header!=null){
????/**
?????????$header?=?array(
??????????????? ?'api-key:'.$key,
??????????????? ?'authorization:'.$authorization,
??????????????????);
????**/
???curl_setopt($ch,?CURLOPT_HTTPHEADER,?$header);
}
$data?=?curl_exec($ch);
curl_close($ch);
if($data?===?FALSE)?
{?
??$data?=?"curl?Error:".curl_error($ch);
}?
return?$data;
}
CURL 发送JSON数据
function?curl_get_request($url,$data=null){
if(is_array($data)){
$data?=?json_encode($data);
}
$ch?=?curl_init();
curl_setopt($ch,?CURLOPT_URL,?$url);
curl_setopt($ch,?CURLOPT_HEADER,?false);//不返回头部信息
if($data!=null){
curl_setopt($ch,?CURLOPT_POSTFIELDS,?$data);
}
curl_setopt($ch,?CURLOPT_RETURNTRANSFER,1);??//结果是否显示出来,1不显示,0显示????
//判断是否https
if(strpos($url,'https://')!==false){
curl_setopt($ch,?CURLOPT_SSL_VERIFYPEER,?false);
curl_setopt($ch,?CURLOPT_SSL_VERIFYHOST,?false);
$UserAgent?=?'Mozilla/4.0?(compatible;?MSIE?7.0;?Windows?NT?6.0;?SLCC1;?.NET?CLR?2.0.50727;?.NET?CLR?3.0.04506;?.NET?CLR?3.5.21022;?.NET?CLR?1.0.3705;?.NET?CLR?1.1.4322)';
curl_setopt($ch,?CURLOPT_USERAGENT,?$UserAgent);//设置Agent
}
curl_setopt($ch,?CURLOPT_HTTPHEADER,
array(
'Content-Type:?application/json;?charset=utf-8',
'Content-Length:'?.?strlen($data))
??);
????
$data?=?curl_exec($ch);
curl_close($ch);
if($data?===?FALSE)?
{?
??$data?=?"curl?Error:".curl_error($ch);
}?
return?$data;
}
文章转自:PHP中用CURL实现GET和POST请求_PHP函数库-考高分网
|