狙击兽

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

PHP三种模拟POST方式总结

  1. <?php  
  2. /* 
  3.  * php模拟post提交[三种方式] 
  4.  */  
  5. $remote_sever = 'http://www.test.com/data.php'; //curl other  
  6. $remote_path = '/data.php';//curl other  
  7.   
  8. $remote_sever = 'www.test.com'; //socket  
  9. $remote_path = '/data.php';//socket  
  10. $post_arr = array(1,3,4,5,7,9,10=>array('name','age','sex'));  
  11. $post_string = dataEncode($post_arr);  
  12.   
  13. //$post_string = "name=stelin&age=16";  
  14. //$post_string = "age=34&name%5B%5D=3&name%5B%5D=4&name%5B%5D=5";  
  15.   
  16. $result = request_by_socket($remote_sever,$remote_path,$post_string);//已通过测试  
  17. //$result = request_by_curl($remote_sever,$post_string); //已通过测试  
  18. //$result = request_by_other($remote_sever,$post_string); //已通过测试  
  19.   
  20. var_dump($result);  
  21.   
  22. /** 
  23.  * Socket版本 
  24.  * 使用方法: 
  25.  * $post_string = "app=socket&version=beta"; 
  26.  * request_by_socket('facebook.cn','/restServer.php',$post_string); 
  27.  */  
  28. function request_by_socket($remote_server, $remote_path, $post_string, $port = 80, $timeout = 30)  
  29. {  
  30.       
  31. //      $poststr    = rtrim($this->dataEncode($heros), '&');  
  32.         $fp = fsockopen($remote_server, 80, $errno, $errstr, 10) or die("$errstr($errno)");  
  33.         fwrite($fp, "POST $remote_path HTTP/1.1\r\n");  
  34.         fwrite($fp, "Host: $remote_server\r\n");  
  35.         fwrite($fp, "Content-type: application/x-www-form-urlencoded\r\n");  
  36.         fwrite($fp, "Content-Length: ".strlen($post_string)."\r\n");  
  37.         fwrite($fp, "Connection: close\r\n\r\n");  
  38.         fwrite($fp, $post_string."\r\n\r\n");  
  39.           
  40.         $result     = '';  
  41.         $isconter   = false;  
  42.         $len        = 0;  
  43.         while($str=fgets($fp))  
  44.         {  
  45.             if($isconter==true) $result .= $str;  
  46.             else if($str=="\r\n")  
  47.             {  
  48.                 $isconter   = true;  
  49.                 if($_SERVER['SERVER_SOFTWARE']!='Microsoft-IIS/6.0') $len       = hexdec(fgets($fp));  
  50.             }  
  51.         }  
  52.         fclose($fp);  
  53.           
  54.         if($_SERVER['SERVER_SOFTWARE']!='Microsoft-IIS/6.0') $result    = substr($result, 0, $len);  
  55.         return $result;  
  56. }   
  57.   
  58.   
  59.   
  60. /** 
  61.  * Curl版本 
  62.  * 使用方法: 
  63.  * $post_string = "app=request&version=beta"; 
  64.  * request_by_curl('http://facebook.cn/restServer.php',$post_string); 
  65.  */  
  66. function request_by_curl($remote_server, $post_string)  
  67. {  
  68.     $ch = curl_init();  
  69.     curl_setopt($ch, CURLOPT_URL, $remote_server);  
  70.     curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);  
  71.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  72.     curl_setopt($ch, CURLOPT_USERAGENT, "Jimmy's CURL Example beta");  
  73.     $data = curl_exec($ch);  
  74.     curl_close($ch);  
  75.     return $data;  
  76. }   
  77.   
  78.   
  79. /** 
  80.  * 其它版本 
  81.  * 使用方法: 
  82.  * $post_string = "app=request&version=beta"; 
  83.  * request_by_other('http://facebook.cn/restServer.php',$post_string); 
  84.  */  
  85. function request_by_other($remote_server, $post_string)  
  86. {  
  87.     $context = array(  
  88.         'http' => array(  
  89.             'method' => 'POST',  
  90.             'header' => 'Content-type: application/x-www-form-urlencoded' .  
  91.                         '\r\n'.'User-Agent : Jimmy\'s POST Example beta' .  
  92.                         '\r\n'.'Content-length:' . strlen($post_string) + 1,  
  93.             'content' =>   $post_string)  
  94.         );  
  95.     $stream_context = stream_context_create($context);  
  96.     $data = file_get_contents($remote_server, false, $stream_context);  
  97.     return $data;  
  98. }  
  99.  /** 
  100.  * POST数据组合,url传递多维数组,格式化 
  101.  * 
  102.  * @internal 
  103.  * @param 数组 $data 
  104.  * @param 字符串 $keyprefix 
  105.  * @param 字符串 $keypostfix 
  106.  * @return 字符串 
  107.  */  
  108. function dataEncode($data, $keyprefix = '', $keypostfix = '')  
  109. {  
  110.     assert(is_array($data));  
  111.     $vars = '';  
  112.     foreach ($data as $key => $value)  
  113.     {  
  114.         if (TRUE == is_array($value)) $vars .= dataEncode($value, $keyprefix . $key . $keypostfix . urlencode('['), urlencode(']'));  
  115.         else $vars .= $keyprefix . $key . $keypostfix . '='.urlencode($value) . '&';  
  116.     }  
  117.     return $vars;  
  118. }   
posted on 2016-05-16 17:05  狙击兽  阅读(579)  评论(0)    收藏  举报