分享

用PHP发送POST请求

 大芬油画 2013-10-29
  1. /**  

  2.  * 发送post请求  

  3.  * @param string $url 请求地址  

  4.  * @param array $post_data post键值对数据  

  5.  * @return string  

  6.  */  

  7. function send_post($url, $post_data) {   

  8.   $postdata = http_build_query($post_data);   


  9.   $options = array(   


  10.     'http' => array(   


  11.       'method' => 'POST',   


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


  13.       'content' => $postdata,   


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


  15.     )   


  16.   );   


  17.   $context = stream_context_create($options);   


  18.   $result = file_get_contents($url, false, $context);   


  19.   


  20.   return $result;   


  21. }   


  22.   


  23. //使用方法   


  24. $post_data = array(   


  25.   'username' => 'stclair2201',   


  26.   'password' => 'handan'  


  27. );   


  28. send_post('http://www.', $post_data);   


  29.   


  30.   


  31.   


  32.   


  33. <?php   


  34. /**  


  35.  * Socket版本  


  36.  * 使用方法:  


  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 (!$socket) die("$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. <?php   


  67. /**   


  68.  * Curl版本   


  69.  * 使用方法:   


  70.  * $post_string = "app=request&version=beta";   


  71.  * request_by_curl('http://www./restServer.php', $post_string);   


  72.  */   


  73. function request_by_curl($remote_server, $post_string) {   


  74.   $ch = curl_init();   


  75.   curl_setopt($ch, CURLOPT_URL, $remote_server);   


  76.   curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);   


  77.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   


  78.   curl_setopt($ch, CURLOPT_USERAGENT, "'s CURL Example beta");   


  79.   $data = curl_exec($ch);   


  80.   curl_close($ch);   


  81.   


  82.   return $data;   


  83. }   


  84. ?>  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约