curl模拟post请求提交

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. header('content-type:text/html;charset=utf-8');  
  2. function curlPost($url,$data,$method){  
  3.     $ch = curl_init();   //1.初始化  
  4.     curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址  
  5.     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式  
  6.     //4.参数如下  
  7.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https  
  8.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
  9.     curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器  
  10.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
  11.     curl_setopt($ch, CURLOPT_AUTOREFERER, 1);  
  12.         curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容  
  13.         curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');  
  14.       
  15.     if($method=="POST"){//5.post方式的时候添加数据  
  16.         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
  17.     }  
  18.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  19.     $tmpInfo = curl_exec($ch);//6.执行  
  20.   
  21.     if (curl_errno($ch)) {//7.如果出错  
  22.         return curl_error($ch);  
  23.     }  
  24.     curl_close($ch);//8.关闭  
  25.     return $tmpInfo;  
  26. }  
  27. $data=array('name' => '1234');  
  28. $url="http://www.sohu.com/";  
  29.   
  30. $method="GET";  
  31. $file=curlPost($url,$data,$method);  
  32. $file=mb_convert_encoding($file,'UTF-8','GBK');  
  33. echo $file;  


当cookie认证登陆的时候

[php]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <?php  
  2.     $cookie_file = tempnam('./temp','cookie');  
  3.     function weixinPost($url,$data,$method,$setcooke=false,$cookie_file=false){  
  4.         $ch = curl_init();   //1.初始化  
  5.         curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址  
  6.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式  
  7.         //4.参数如下      
  8.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  
  9.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
  10.         curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');  
  11.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
  12.         curl_setopt($ch, CURLOPT_AUTOREFERER, 1);  
  13.           
  14.         if($method=="POST"){//5.post方式的时候添加数据     
  15.             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
  16.         }  
  17.         if($setcooke==true){  
  18.             curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);  
  19.         }else{  
  20.             curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);  
  21.         }  
  22.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  23.         $tmpInfo = curl_exec($ch);//6.执行  
  24.   
  25.         if (curl_errno($ch)) {//7.如果出错  
  26.             return curl_error($ch);  
  27.         }  
  28.         curl_close($ch);//8.关闭  
  29.         return $tmpInfo;  
  30.     }  
  31.     $data=array('username' => '***','password'=>'***');  
  32.     $url="http://www.xinxinj.com/login.php";  
  33.     $method="POST";  
  34.     $file=weixinPost($url,$data,$method,true,$cookie_file);  
  35.     echo $file;  
  36.           
  37.     $url="http://www.xinxinj.com/admin.php";  
  38.     $method="GET";  
  39.     $file=weixinPost($url,$data,$method,false,$cookie_file);  
  40.     echo $file;  
  41.           
  42. ?>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值