curl 模仿浏览器post 访问

header('content-type:text/html;charset=utf-8');
        //传值三个参数      访问路径  数值   方式
        function curlPost($url,$data,$method){  
            $ch = curl_init();   //1.初始化  
            curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址  
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式  
            //4.参数如下  
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https  
            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);  
            curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容  
            curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');  
              
            if($method=="POST"){//5.post方式的时候添加数据  
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
            }  
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
            $tmpInfo = curl_exec($ch);//6.执行  
          
            if (curl_errno($ch)) {//7.如果出错  
                return curl_error($ch);  
            }  
            curl_close($ch);//8.关闭  
            return $tmpInfo;  
        }
        //传值数组
        $data=array('name' => '1234');
        //路径
        $url="http://blog.jobbole.com/tag/php/";  
        //访问方式
        $method="GET";
        //调用传值
        $file=curlPost($url,$data,$method);
        //这个函数进行转码
        $file=mb_convert_encoding($file,'UTF-8','GBK');  

        echo $file;



当cookie认证登陆的时候

  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. ?> 



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值