PHP發送HTTP請求(POST請求、GET請求)

file_get_contents版本:

01 /**
02  * 發送post請求
03  * @param string $url 請求地址
04  * @param array $post_data post鍵值對數據
05  * @return string
06  */
07 function send_post($url$post_data) {
08  
09     $postdata = http_build_query($post_data);
10     $options array(
11         'http' =>; array(
12             'method' =>; 'POST',
13             'header' =>; 'Content-type:application/x-www-form-urlencoded',
14             'content' =>; $postdata,
15             'timeout' =>; 15 * 60 // 超時時間(單位:s)
16         )
17     );
18     $context = stream_context_create($options);
19     $result file_get_contents($url, false, $context);
20  
21     return $result;
22 }

使用如下:

1 post_data = array(
2     'username' => 'stclair2201',
3     'password' => 'handan'
4 );
5 send_post('http://blog.snsgou.com'$post_data);

 

實戰經驗:

當我利用上述代碼給另一臺服務器發送http請求時,發現,如果服務器處理請求時間過長,本地的PHP會中斷請求,即所謂的超時中斷,第一個懷疑的是PHP本身執行時間的超過限制,但想想也不應該,因爲老早就按照這篇文章設置了“PHP執行時間限制”(【推薦】PHP上傳文件大小限制大全 ),仔細琢磨,想想,應該是http請求本身的一個時間限制,於是乎,就想到了怎麼給http請求時間限制搞大一點。。。。。。查看PHP手冊,果真有個參數 “ timeout ”,默認不知道多大,當把它的值設大一點,問題得已解決,弱弱地做個筆記~~~

 

Socket版本:

01 /**
02  * Socket版本
03  * 使用方法:
04  * $post_string = "app=socket&version=beta";
05  * request_by_socket('blog.snsgou.com', '/restServer.php', $post_string);
06  */
07 function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {
08     $socket fsockopen($remote_server$port$errno$errstr$timeout);
09     if (!$socketdie("$errstr($errno)");
10     fwrite($socket"POST $remote_path HTTP/1.0");
11     fwrite($socket"User-Agent: Socket Example");
12     fwrite($socket"HOST: $remote_server");
13     fwrite($socket"Content-type: application/x-www-form-urlencoded");
14     fwrite($socket"Content-length: " . (strlen($post_string) + 8) . "");
15     fwrite($socket"Accept:*/*");
16     fwrite($socket"");
17     fwrite($socket"mypost=$post_string");
18     fwrite($socket"");
19     $header "";
20     while ($str = trim(fgets($socket, 4096))) {
21         $header .= $str;
22     }
23  
24     $data "";
25     while (!feof($socket)) {
26         $data .= fgets($socket, 4096);
27     }
28  
29     return $data;
30 }

 

Curl版本:

01 /**
02  * Curl版本
03  * 使用方法:
04  * $post_string = "app=request&version=beta";
05  * request_by_curl('http://blog.snsgou.com/restServer.php', $post_string);
06  */
07 function request_by_curl($remote_server$post_string) {
08     $ch = curl_init();
09     curl_setopt($ch, CURLOPT_URL, $remote_server);
10     curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' $post_string);
11     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
12     curl_setopt($ch, CURLOPT_USERAGENT, "snsgou.com's CURL Example beta");
13     $data = curl_exec($ch);
14     curl_close($ch);
15  
16     return $data;
17 }

 

Curl版本(2)

01 /**
02  * 發送HTTP請求
03  *
04  * @param string $url 請求地址
05  * @param string $method 請求方式 GET/POST
06  * @param string $refererUrl 請求來源地址
07  * @param array $data 發送數據
08  * @param string $contentType
09  * @param string $timeout
10  * @param string $proxy
11  * @return boolean
12  */
13 function send_request($url$data$refererUrl ''$method 'GET'$contentType 'application/json'$timeout= 30, $proxy = false) {
14     $ch = null;
15     if('POST' === strtoupper($method)) {
16         $ch = curl_init($url);
17         curl_setopt($ch, CURLOPT_POST, 1);
18         curl_setopt($ch, CURLOPT_HEADER,0 );
19         curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
20         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
21         curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
22         curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
23         if ($refererUrl) {
24             curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
25         }
26         if($contentType) {
27             curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
28         }
29         if(is_string($data)){
30             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
31         else {
32             curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
33         }
34     else if('GET' === strtoupper($method)) {
35         if(is_string($data)) {
36             $real_url $url. (strpos($url'?') === false ? '?' ''). $data;
37         else {
38             $real_url $url. (strpos($url'?') === false ? '?' ''). http_build_query($data);
39         }
40  
41         $ch = curl_init($real_url);
42         curl_setopt($ch, CURLOPT_HEADER, 0);
43         curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:'.$contentType));
44         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
45         curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
46         if ($refererUrl) {
47             curl_setopt($ch, CURLOPT_REFERER, $refererUrl);
48         }
49     else {
50         $args = func_get_args();
51         return false;
52     }
53  
54     if($proxy) {
55         curl_setopt($ch, CURLOPT_PROXY, $proxy);
56     }
57     $ret = curl_exec($ch);
58     $info = curl_getinfo($ch);
59     $contents array(
60             'httpInfo' => array(
61                     'send' => $data,
62                     'url' => $url,
63                     'ret' => $ret,
64                     'http' => $info,
65             )
66     );
67  
68     curl_close($ch);
69     return $ret;
70 }

調用 WCF接口 的一個例子:$json = restRequest($r_url,'POST', json_encode($data));

轉自http://blog.snsgou.com/blog/161.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章