php curl 实例

  1转自phpchina 一个人的回答,又搜不到了。。。。   做模拟登录时火狐浏览器的Httpfox和Firebug两个插件真心好使啊,嗯,神器
  2
  3 
  4 /*------------------------简单的测试------------------------------------------------start*/
  5   /*
  6   $ch = curl_init();                      //开启curl语柄
  7 
  8   curl_setopt($ch, CURLOPT_URL, "http://localhost/curl_server/index.php?id=1");      //设置提交链接
  9   curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);                                        //设置服务端返回的数据是否返回数据 0为不返回
 10                                                                                      //                                 1为返回
 11   curl_setopt($ch, CURLOPT_HEADER, 0);                                               //是否返回相应头信息   0为不返回   1为返回
 12   $output = curl_exec($ch);
 13   echo $output."============";
 14 
 15 
 16 //  测试链接是否可用
 17 // curl_exec($ch);
 18 // $data=curl_getinfo($ch);
 19 // if($data['http_code']==404){
 20 //           echo "server error";
 21 // }
 22 */
 23 /*----------------------简单的测试------------------------------------------------------end*/
 24 
 25 
 26 /*------------------------模拟post提交------------------------------------------------start*/
 27 /*
 28 header("Content-Type: text/html; charset=utf-8");
 29 $post_data=array('user_name'=>"卫江涛",
 30                  'user_pass'=>"超级魂斗罗加强版");
 31 
 32 $ch = curl_init();
 33 curl_setopt($ch, CURLOPT_URL, "http://localhost/curl_server/index.php");
 34 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
 35 curl_setopt($ch, CURLOPT_HEADER, 0);
 36 curl_setopt($ch,CURLOPT_POST,1);                                                   // 是否使用post提交
 37 curl_setopt($ch,CURLOPT_POSTFIELDS, $post_data);                                   // post所要提交的数据
 38 
 39 $output=curl_exec($ch);
 40 $return_data=json_decode($output,true);
 41 var_dump($return_data);
 42 */
 43 /*
 44 */
 45 /*------------------------模拟post提交------------------------------------------------end*/
 46 
 47 
 48 /*----------------------图片上传------------------------------------------------------end*/
 49 /*
 50 $post_data=array('user_name'=>"卫江涛",
 51                  'fff'=>'@F:\SteelLand\apache_web\curl_client\1.jpg'            //此处必须为绝对路径
 52                  );
 53 $ch = curl_init();
 54 curl_setopt($ch, CURLOPT_URL, "http://localhost/curl_server/index.php");
 55 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
 56 curl_setopt($ch, CURLOPT_HEADER, 0);
 57 curl_setopt($ch,CURLOPT_POST,1);                                                   // 是否使用post提交
 58 curl_setopt($ch,CURLOPT_POSTFIELDS, $post_data);                                   // post所要提交的数据
 59 
 60 $output=curl_exec($ch);
 61 
 62 var_dump($output);
 63 */
 64 
 65 /*----------------------图片上传------------------------------------------------------end*/
 66 
 67 /*----------------------curl模拟登录------------------------------------------------------start*/
 68 header("Content-Type: text/html; charset=utf-8");
 69 
 70 //第一步生成cookie
 71 //$cookie_file = tempnam('./temp','cookie');     //生成cookie文件存放sessionid
 72 /*
 73 $ch = curl_init();
 74 curl_setopt($ch, CURLOPT_URL, "http://localhost/curl_server/index.php");
 75 curl_setopt($ch, CURLOPT_HEADER, 0);
 76 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 77 curl_setopt($ch, CURLOPT_POST, 1);
 78 curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);  //当连接关闭后 存放cookie的文件
 79 curl_exec($ch);
 80 curl_close($ch);
 81 */
 82 
 83 
 84 //第二步利用生成的cookie进行登录  /*此处的文件路径必须为绝对路径  否则失效*/
 85 /*
 86 $cookie_file = 'F:\SteelLand\apache_web\curl_client\temp\coo27D1.tmp';
 87 $ch = curl_init();
 88 curl_setopt($ch, CURLOPT_URL, "http://localhost/curl_server/index1.php");
 89 curl_setopt($ch, CURLOPT_HEADER, 0);
 90 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 91 curl_setopt($ch, CURLOPT_POST, 1);
 92 curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);  //设置使用cookie的文件
 93 
 94 $output=curl_exec($ch);
 95 curl_close($ch);
 96 
 97 echo $output;
 98 
 99 */
100 /*
101 $json=json_encode(array("a"=>"b","c"=>"d"));
102 $post_data=array('user_name'=>"卫江涛",
103                  'user_pass'=>$json);
104 
105 $ch = curl_init();
106 curl_setopt($ch, CURLOPT_URL, "http://localhost/curl_server/index.php");
107 curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
108 curl_setopt($ch, CURLOPT_HEADER, 0);
109 curl_setopt($ch,CURLOPT_POST,1);                                                   // 是否使用post提交
110 curl_setopt($ch,CURLOPT_POSTFIELDS, $post_data);                                   // post所要提交的数据
111 
112 $output=curl_exec($ch);
113 echo $output;
114 */
115 
116 //$data = array("name" => "Hagrid", "age" => "36");
117         //$data_string = json_encode($data);
118     $data_string="<?xml version='1.0' encoding='UTF-8'?><message>dfdffdf</message>";
119 
120         $ch = curl_init('http://localhost/curl_server/index.php');
121         //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
122         curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
123         curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
124         curl_setopt($ch, CURLOPT_HTTPHEADER, array(
125             'Content-Type: text/xml',
126             'Content-Length: ' . strlen($data_string))
127         );
128       curl_setopt($ch, CURLOPT_USERAGENT,"xxxxxxxxxxxxx");
129         $result = curl_exec($ch);
130 
131     echo $result;
132 /*----------------------curl模拟登录------------------------------------------------------end*/
133 ?>

 

posted @ 2013-06-02 14:54  舟了个舟  阅读(328)  评论(0编辑  收藏  举报