Home > Backend Development > PHP Tutorial > PHP sends POST request

PHP sends POST request

WBOY
Release: 2016-07-29 09:02:06
Original
995 people have browsed it
  1. /**
  2. * Send post request
  3. * @param string $url Request address
  4. * @param array $post_data post key-value pair data
  5. * @return string
  6. */  
  7. function send_post($url$post_data) {  
  8.   
  9.   $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. }  
  23.   
  24. //使用方法  
  25. $post_data = array(  
  26.   'username' => 'stclair2201',  
  27.   'password' => 'handan'  
  28. );  
  29. send_post('http://www.qianyunlai.com'$post_data);  
  30.   
  31.   
  32.   
  33.   
  34. /**
  35. * Socket version
  36. * How to use:
  37. * $post_string = "app=socket&version=beta";
  38. * request_by_socket('chajia8.com ', '/restServer.php', $post_string);
  39. */  
  40. function request_by_socket($remote_server,$remote_path,$post_string,$port = 80,$timeout = 30) {  
  41.   $socket = fsockopen($remote_server$port$errno$errstr$timeout);  
  42.   if (!$socketdie("$errstr($errno)");  
  43.   fwrite($socket"POST $remote_path HTTP/1.0");  
  44.   fwrite($socket"User-Agent: Socket Example");  
  45.   fwrite($socket"HOST: $remote_server");  
  46.   fwrite($socket"Content-type: application/x-www-form-urlencoded");  
  47.   fwrite($socket"Content-length: " . (strlen($post_string) + 8) . "");  
  48.   fwrite($socket"Accept:*/*");  
  49.   fwrite($socket"");  
  50.   fwrite($socket"mypost=$post_string");  
  51.   fwrite($socket"");  
  52.   $header = "";  
  53.   while ($str = trim(fgets($socket, 4096))) {  
  54.     $header .= $str;  
  55.   }  
  56.   
  57.   $data = "";  
  58.   while (!feof($socket)) {  
  59.     $data .= fgets($socket, 4096);  
  60.   }  
  61.   
  62.   return $data;  
  63. }  
  64. ?>  
  65.   
  66. /**
  67. * Curl version
  68. * How to use:
  69. * $post_string = "app=request&version=beta";
  70. * request_by_curl('http://www. qianyunlai.com/restServer.php', $post_string);
  71. */  
  72. function request_by_curl($remote_server$post_string) {  
  73.   $ch = curl_init();  
  74.   curl_setopt($ch, CURLOPT_URL, $remote_server);  
  75.   curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);  
  76.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  77.   curl_setopt($ch, CURLOPT_USERAGENT, "qianyunlai.com's CURL Example beta");  
  78.   $data = curl_exec($ch);  
  79.   curl_close($ch);  
  80.   
  81.   return $data;  
  82. }  
  83. ?>  

原文地址:http://blog.sjzycxx.cn/post/435/

以上就介绍了PHP发送POST请求,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template