工程是一门科学,科学是严谨的。

PHP模拟POST请求,获取response内容

 1     /*
 2      * 模拟POST请求,获取response内容
 3      */
 4     protected function curl($url, $type, $header, $data) {
 5         $CURL_OPTS = array (
 6                 CURLOPT_CONNECTTIMEOUT => 10,
 7                 CURLOPT_RETURNTRANSFER => true,
 8                 CURLOPT_SSL_VERIFYPEER => false,
 9                 CURLOPT_TIMEOUT => 60,
10                 CURLOPT_USERAGENT => 'simulate submit' 
11         );
12         $opts = $CURL_OPTS;
13         $opts [CURLOPT_URL] = $url;
14         $opts [CURLOPT_CUSTOMREQUEST] = $type;
15         $header [] = 'Expect:';
16         $opts [CURLOPT_HTTPHEADER] = $header;
17         if ($type == 'POST' || $type == 'PUT') {
18             $opts [CURLOPT_POSTFIELDS] = $data;
19         }
20         
21         $ch = curl_init ();
22         curl_setopt_array ( $ch, $opts );
23         $result = curl_exec ( $ch );
24         if (curl_errno ( $ch )) {
25             die ( 'CURL error: ' . curl_error ( $ch ) );
26         }
27         curl_close ( $ch );
28         return $result;
29     }

参数: 

$header = array (
    'Content_type: application/x-www-form-urlencoded'
  );

$data="param1=qwer&param2=asdf&param3=jkl";

posted @ 2013-06-25 22:32  大圣的笑  阅读(10798)  评论(0编辑  收藏  举报