调试时经常要模拟提交,在抓取别人页面时也经常要去请求别人的页面,于是就写了下面这个类。封装了三种post提交方法和一个request请求方法,

  1. <?php 
  2. /** 
  3.  *  HTTP常用请求封装 
  4.  * 
  5.  *  Copyright(c) 2012 by ustb80. All rights reserved 
  6.  * 
  7.  *  To contact the author write to {@link mailto:ustb80@163.com} 
  8.  * 
  9.  * @author ustb80 
  10.  * @version $Id: HttpHelper.php,v 1.0 2012-8-9 
  11.  * @package library 
  12.  */ 
  13.  
  14.  
  15. // ------------------------------------------------------------------------ 
  16.  
  17. /** 
  18.  * http请求处理 
  19.  * 
  20.  * 开发中经常需要模拟提交请求,本类封装了常用的post方法 
  21.  * 
  22.  * @author ustb80 
  23.  * 
  24.  */ 
  25. class HttpHelper 
  26. { 
  27.     // 当前的user-agent字符串 
  28.     public $ua_string= "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1"; 
  29.  
  30.     // 支持的提交方式 
  31.     public $post_type_list = array("curl", "socket", "stream"); 
  32.  
  33.     // 本地cookie文件 
  34.     private $cookie_file; 
  35.  
  36.     // -------------------------------------------------------------------- 
  37.  
  38.     /** 
  39.      * 构造函数 
  40.      * 
  41.      * @param array $params 初始化参数 
  42.      */ 
  43.     public function __construct($params = array()) 
  44.     { 
  45.         if(count($params) > 0) 
  46.         { 
  47.             $this->init($params); 
  48.         } 
  49.     } 
  50.  
  51.     // -------------------------------------------------------------------- 
  52.  
  53.     /** 
  54.      * 参数初始化 
  55.      * 
  56.      * @param array $params 
  57.      */ 
  58.     public function init($params) 
  59.     { 
  60.         if(count($params) > 0) 
  61.         { 
  62.             foreach($params as $key => $val) 
  63.             { 
  64.                 if(isset($this->$key)) 
  65.                 { 
  66.                     $this->$key = $val; 
  67.                 } 
  68.             } 
  69.         } 
  70.     } 
  71.  
  72.     // -------------------------------------------------------------------- 
  73.  
  74.     /** 
  75.      * 提交请求 
  76.      * 
  77.      * @param string $url 请求地址 
  78.      * @param mixed $data 提交的数据 
  79.      * @param string $type 提交类型,curl,socket,stream可选 
  80.      */ 
  81.     public function post($url, $data, $type = "socket") 
  82.     { 
  83.         if(!in_array($type, $this->post_type_list)) 
  84.         { 
  85.             die("undefined post type"); 
  86.         } 
  87.         $function_name = $type . "Post"; 
  88.         return call_user_func_array(array($this, $function_name), array($url, $data)); 
  89.     } 
  90.  
  91.     // -------------------------------------------------------------------- 
  92.  
  93.     /** 
  94.      * 更改默认的ua信息 
  95.      * 
  96.      * 本方法常用于模拟各种浏览器 
  97.      * 
  98.      * @param string $ua_string UA字符串 
  99.      */ 
  100.     public function setUA($user_agent) 
  101.     { 
  102.         $this->ua_string = $user_agent; 
  103.         return $this; 
  104.     } 
  105.  
  106.     // -------------------------------------------------------------------- 
  107.  
  108.     /** 
  109.      * 设置本地cookie文件 
  110.      * 
  111.      * 在用curl来模拟时常需要设置此项 
  112.      * 
  113.      * @param string $cookie_file 文件路径 
  114.      */ 
  115.     public function setCookieFile($cookie_file) 
  116.     { 
  117.         $this->cookie_file = $cookie_file; 
  118.         return $this; 
  119.     } 
  120.  
  121.     // -------------------------------------------------------------------- 
  122.  
  123.     /** 
  124.      * curl方式提交 
  125.      * 
  126.      * @param string $url 请求地址 
  127.      * @param mixed $data 提交的数据 
  128.      * @param string $user_agent 自定义的UA 
  129.      * @return mixed 
  130.      */ 
  131.     public function curlPost($url, $data, $user_agent = '') 
  132.     { 
  133.         if($user_agent == '') 
  134.         { 
  135.             $user_agent = $this->ua_string; 
  136.         } 
  137.  
  138.         if (!is_array($data)) 
  139.         { 
  140.             $data = array($data); 
  141.         } 
  142.  
  143.         $data = http_build_query($data); 
  144.  
  145.         if (!function_exists("curl_init")) 
  146.         { 
  147.             die('undefined function curl_init'); 
  148.         } 
  149.  
  150.         $ch = curl_init(); 
  151.         curl_setopt($ch, CURLOPT_URL, $url); 
  152.         curl_setopt($ch, CURLOPT_POST, true); 
  153.         curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
  154.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  155.         curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); 
  156.         $rs = curl_exec($ch); 
  157.         curl_close($ch); 
  158.         return $rs; 
  159.     } 
  160.  
  161.     // -------------------------------------------------------------------- 
  162.  
  163.     /** 
  164.      * 套接字提交 
  165.      * 
  166.      * @param string $url 请求地址 
  167.      * @param mixed $data 提交的数据 
  168.      * @param string $user_agent 自定义的UA 
  169.      * @param int $port 端口 
  170.      * @param int $timeout 超时限制 
  171.      * @return mixed 
  172.      */ 
  173.     public function socketPost($url, $data, $user_agent = '', $port = 80, $timeout = 30) 
  174.     { 
  175.         $url_info = parse_url($url); 
  176.         $remote_server = $url_info['host']; 
  177.         $remote_path = $url_info['path']; 
  178.         $socket = fsockopen($remote_server, $port, $errno, $errstr, $timeout); 
  179.         if(!$socket) 
  180.         { 
  181.             die("$errstr($errno)"); 
  182.         } 
  183.  
  184.         if($user_agent == '') 
  185.         { 
  186.             $user_agent = $this->ua_string; 
  187.         } 
  188.  
  189.         if (!is_array($data)) 
  190.         { 
  191.             $data = array($data); 
  192.         } 
  193.  
  194.         $data = http_build_query($data); 
  195.  
  196.         fwrite($socket, "POST {$remote_path} HTTP/1.0\r\n"); 
  197.         fwrite($socket, "User-Agent: {$user_agent}\r\n"); 
  198.         fwrite($socket, "HOST: {$remote_server}\r\n"); 
  199.         fwrite($socket, "Content-type: application/x-www-form-urlencoded\r\n"); 
  200.         fwrite($socket, "Content-length: " . strlen($data) . "\r\n"); 
  201.         fwrite($socket, "Accept:*/*\r\n"); 
  202.         fwrite($socket, "\r\n"); 
  203.         fwrite($socket, "{$data}\r\n"); 
  204.         fwrite($socket, "\r\n"); 
  205.  
  206.         $header = ""; 
  207.         while($str = trim(fgets($socket, 4096))) 
  208.         { 
  209.             $header .= $str; 
  210.         } 
  211.  
  212.         $data = ""; 
  213.         while(!feof($socket)) 
  214.         { 
  215.             $data .= fgets($socket, 4096); 
  216.         } 
  217.  
  218.         return $data; 
  219.     } 
  220.  
  221.     // -------------------------------------------------------------------- 
  222.  
  223.     /** 
  224.      * 文件流提交 
  225.      * 
  226.      * @param string $url 提交地址 
  227.      * @param string $data 数据 
  228.      * @param string $user_agent 自定义的UA 
  229.      * @return mixed 
  230.      */ 
  231.     public function streamPost($url, $data, $user_agent = '') 
  232.     { 
  233.         if($user_agent == '') 
  234.         { 
  235.             $user_agent = $this->ua_string; 
  236.         } 
  237.  
  238.         if (!is_array($data)) 
  239.         { 
  240.             $data = array($data); 
  241.         } 
  242.  
  243.         $data = http_build_query($data); 
  244.         $context = array( 
  245.                 'http' => array( 
  246.                         'method' => 'POST', 
  247.                         'header' => 'Content-type: application/x-www-form-urlencoded' . "\r\n" . 'User-Agent : ' . $user_agent . "\r\n" . 'Content-length: ' . strlen($data), 
  248.                         'content' => $data 
  249.                 ) 
  250.         ); 
  251.         $stream_context = stream_context_create($context); 
  252.         $data = file_get_contents($url, FALSE, $stream_context); 
  253.         return $data; 
  254.     } 
  255.  
  256.     // -------------------------------------------------------------------- 
  257.  
  258.     /** 
  259.      * 发送请求 
  260.      * 
  261.      * 本方法通过curl函数向目标服务器发送请求 
  262.      * 
  263.      * @param string $url 请求地址 
  264.      * @return mixed 
  265.      */ 
  266.     public function request($url) 
  267.     { 
  268.         $ch = curl_init(); 
  269.         curl_setopt($ch, CURLOPT_URL, $url); 
  270.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  271.         curl_setopt($ch, CURLOPT_USERAGENT, !empty($this->ua_string)? $this->ua_string : $_SERVER['HTTP_USER_AGENT']); 
  272.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
  273.  
  274.         if (isset($this->cookie_file)) 
  275.         { 
  276.             curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_file); 
  277.         } 
  278.         $data = curl_exec($ch); 
  279.         curl_close($ch); 
  280.  
  281.         return $data; 
  282.     } 
  283. } 

例子:

  1. <?php 
  2. require_once 'HttpHelper.php'; 
  3. $HttpHelper = new HttpHelper(); 
  4.  
  5. $url = "http://localhost/post.php"; 
  6.  
  7. $data = array("name"=>"socket"); 
  8. $rs[] = $HttpHelper->post($url, $data); 
  9.  
  10. $data = array("name"=>"curl"); 
  11. $rs[] = $HttpHelper->post($url, $data, "curl"); 
  12.  
  13. $data = array("name"=>"stream"); 
  14. $rs[] = $HttpHelper->post($url, $data, "stream"); 
  15.  
  16. $rs[] = $HttpHelper->request($url); 
  17.  
  18. print_r($rs); 

post.php代码如下:

  1. <?php 
  2. echo 'test request:'; 
  3. print_r($_REQUEST); 

输出结果:

  1. Array 
  2. ( 
  3.     [0] => test request:Array 
  4. ( 
  5.     [name] => socket 
  6. ) 
  7.  
  8.     [1] => test request:Array 
  9. ( 
  10.     [name] => curl 
  11. ) 
  12.  
  13.     [2] => test request:Array 
  14. ( 
  15.     [name] => stream 
  16. ) 
  17.  
  18.     [3] => test request:Array 
  19. ( 
  20. ) 
  21.  
  22. )