Home  >  Article  >  Backend Development  >  PHP implements post and get

PHP implements post and get

WBOY
WBOYOriginal
2016-07-30 13:30:351088browse

file_get_contents版本:

01 <?php

02/**

03 * Send post request

04 * @param string $url request address

05* @param array $post_data post key-value pair data

06* @return string

07*/

08functionsend_post($url, $post_data) {

09 

10    $postdata= http_build_query($post_data);

11    $options= array(

12        'http'=> array(

13             'method'=> 'POST',

14             'header'=> 'Content-type:application/x-www-form-urlencoded',

15             'content'=> $postdata,

16             'timeout'=> 15 * 60 // 超时时间(单位:s)

17         )

18     );

19     $context= stream_context_create($options);

20     $result= file_get_contents($url, false, $context);

21  

22     return$result;

23 }

使用如下:

1 $post_data= array(

2     'username'=> 'stclair2201',

3     'password'=> 'handan'

4 );

5 send_post('http://blog.snsgou.com', $post_data);

Practical experience:

When I used the above code to send an http request to another server, I found that if the server takes too long to process the request, the local PHP will interrupt the request, which is the so-called timeout interrupt. The first one I suspect that the execution time of PHP itself exceeds the limit, but I shouldn’t think about it because I have set the “PHP execution time limit” according to this article a long time ago ([Recommended] PHP upload file size limit list). After careful consideration, I thought I thought it should be a time limit on the http request itself, so I thought of how to increase the time limit on the http request. . . . . . Check the PHP manual, and there is indeed a parameter "timeout", I don't know how big it is by default. When you set its value to a larger value, the problem will be solved. Let me make a note~~~

Socket version:

01 /**

02 * Socket version

03 * How to use:

04 * $post_string = "app=socket&version=beta";

05 * request_by_socket( 'blog.snsgou.com', '/restServer.php', $post_string);

06 */

07 functionrequest_by_socket($remote_server ,$remote_path,$ post_string,$port= 80,$timeout= 30) {

08 $socket= fsockopen ($remote_server, $port, $errno, $errstr, $timeout);

09 if(!$socket) die("$errstr($errno)");

10 fwrite($socket, "POST $remote_path HTTP/1.0");

11, 12
$socket "User-Agent: Socket Example");

et, );
"HOST: $remote_server" 13
fwrite(

$sock et, ); $post_string) + 8) .
"Content-type: application/x-www-form-urlencoded" $socket, "Content-length: ". (strlen(
""

);16
15 "Accept:*/*");
)

fwrite($socket ""17
, );

"mypost=$post_string");fwrite($socket
18
,

"");$header=
19
""

;

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 functionrequest_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 request address

* @param string $refererUrl request source address
05 * @param string $method Request method GET/POST

* @param array $data Send data
07

* @param string $contentType
08

* @param string $timeout
09

* @param string $proxy
10

* @return boolean
11

*/
12

send_request(
13function
$url

,

$data​ $ch
, $refererUrl = '', $method= 'GET', $contentType= 'application/json', $timeout= 30, $proxy= false) {14
= null;

if
15​​
(

'POST'

===
strtoupper( $method )) { 16                                                                                                                                                                 
17

                                                                             $ch
, CURLOPT_POST, 1); 18
, CURLOPT_HEADER,0 );

, CURLOPT_RETURNTRANSFER, 1);
~ 20                                                                    ch

21
$ch, CURLOPT_FORBID_REUSE, 1);

22                                                                                                                                                                curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

23 $refererUrl) {

24
                                                                                                                                     $refererUrl);

25

) {
26 ($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     } elseif('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         returnfalse;

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));

以上就介绍了php实现post和get,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn