首页 > 代码库 > CURL 模拟http提交

CURL 模拟http提交

 

1:CURL模拟get提交

private function httpGet($url) {
   $curl = curl_init();
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($curl, CURLOPT_TIMEOUT, 500);
   curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
   curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
   curl_setopt($curl, CURLOPT_URL, $url);

   $res = curl_exec($curl);
   curl_close($curl);

   return $res;
}

2:CURL模拟post提交

//模拟post提交
function http_request($url , $data , $method="POST"){
$ch = curl_init(); //初始化
curl_setopt($ch , CURLOPT_URL , $url); //请求地址
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //请求方式
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, ‘Mozilla/5.0 (compatible; MSIE 5.01; windows NT 5.0)‘);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
if($method == ‘POST‘){
//POST方式的时候添加数据
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$tmpinfo = curl_exec($ch); //执行
if(curl_errno($ch)){
return curl_error($ch);
}
curl_close($ch); //关闭
return $tmpinfo;
}

curl模拟post提交
function https_request($url, $data = null){
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
     if (!empty($data)){
          curl_setopt($curl, CURLOPT_POST, 1);
          curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
     }
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     $output = curl_exec($curl);
     curl_close($curl);
     return $output;
}

 

CURL 模拟http提交