且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

发布一个http请求封装类

更新时间:2022-07-01 13:07:35

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

例子:


  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.     [0] => test request:Array 
  3.     [name] => socket 
  4.  
  5.     [1] => test request:Array 
  6.     [name] => curl 
  7.  
  8.     [2] => test request:Array 
  9.     [name] => stream 
  10.  
  11.     [3] => test request:Array 
  12.  









本文转自 ustb80 51CTO博客,原文链接:http://blog.51cto.com/ustb80/1043797,如需转载请自行联系原作者