zoukankan      html  css  js  c++  java
  • curl模拟post请求

    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认证登陆的时候

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

  • 相关阅读:
    根据租户id获取部门树状结构有父子结构的数据list
    JAVA 中 Map 与实体类相互转换的简单方法
    JAVA 实体类List<Entity >转 List<Map>
    在eclipse中怎样查找一个类中的方法在其他哪个类中被调用了?快捷键是什么?
    eclipse项目包层级显示方式调整
    怎样查看JDK是32位还是64位
    python 面向对象专题(十三):元类(二): metaclass魔术方法
    python 面向对象专题(十二):元类(一): metaclass概述
    Hive高级(7):优化(7) 数据倾斜问题剖析
    数据可视化基础专题(二十七):numpy80题(六)NumPy进阶修炼第四期|NumPy最后二十问
  • 原文地址:https://www.cnblogs.com/hehe520/p/6147495.html
Copyright © 2011-2022 走看看