Home >Backend Development >PHP Tutorial >php httpClient class

php httpClient class

WBOY
WBOYOriginal
2016-07-25 08:43:311467browse
  1. class httpClient {
  2. public $buffer = null; // buffer gets the returned string
  3. public $referer = null; // referer sets the URL of HTTP_REFERER
  4. public $response = null ; // response header information of the server response
  5. public $request = null; // request header information sent to the server
  6. private $args = null;
  7. public static function init(&$instanceof, $args = array()) {
  8. return $instanceof = new self($args);
  9. }
  10. private function __construct($args = array()) {
  11. if(!is_array($args)) $args = array();
  12. $this ->args = $args;
  13. if(!empty($this->args['debugging'])) {
  14. ob_end_clean();
  15. set_time_limit(0);
  16. header('Content-Type: text/plain ; charset=utf-8');
  17. }
  18. }
  19. public function get($url, $data = null, $cookie = null) {
  20. $parse = parse_url($url);
  21. $url .= isset($parse['query']) ? '&'. $data : ( $data ? '?'. $data : '' );
  22. $host = $parse['host'];
  23. $header = 'Host: '. $host. "\r\n";
  24. $header .= 'Connection: close'. "\r\n";
  25. $header .= 'Accept: */*'. "\r\ n";
  26. $header .= 'User-Agent: '. ( isset($this->args['userAgent']) ? $this->args['userAgent'] : $_SERVER['HTTP_USER_AGENT'] ). "\r\n";
  27. $header .= 'DNT: 1'. "\r\n";
  28. if($cookie) $header .= 'Cookie: '. $cookie. "\r\n ";
  29. if($this->referer) $header .= 'Referer: '. $this->referer. "\r\n";
  30. $options = array();
  31. $options['http ']['method'] = 'GET';
  32. $options['http']['header'] = $header;
  33. $response = get_headers($url);
  34. $this->request = $header ;
  35. $this->response = implode("\r\n", $response);
  36. $context = stream_context_create($options);
  37. return $this->buffer = file_get_contents($url, false, $context );
  38. }
  39. public function post($url, $data = null, $cookie = null) {
  40. $parse = parse_url($url);
  41. $host = $parse['host'];
  42. $header = 'Host: '. $host. "\r\n";
  43. $header .= 'Connection: close'. "\r\n";
  44. $header .= 'Accept: */*'. " \r\n";
  45. $header .= 'User-Agent: '. ( isset($this->args['userAgent']) ? $this->args['userAgent'] : $_SERVER[' HTTP_USER_AGENT'] ). "\r\n";
  46. $header .= 'Content-Type: application/x-www-form-urlencoded'. "\r\n";
  47. $header .= 'DNT: 1' . "\r\n";
  48. if($cookie) $header .= 'Cookie: '. $cookie. "\r\n";
  49. if($this->referer) $header .= 'Referer: '. $this->referer. "\r\n";
  50. if($data) $header .= 'Content-Length: '. strlen($data). "\r\n";
  51. $options = array();
  52. $options['http']['method'] = 'POST';
  53. $options['http']['header'] = $header;
  54. if($data) $options[' http']['content'] = $data;
  55. $response = get_headers($url);
  56. $this->request = $header;
  57. $this->response = implode("\r\n" , $response);
  58. $context = stream_context_create($options);
  59. return $this->buffer = file_get_contents($url, false, $context);
  60. }
  61. }
  62. httpClient::init($httpClient , array( 'debugging' => true , 'userAgent' => 'MSIE 15.0' ));
  63. $httpClient->get('http://www.baidu.com', 'name=haowei') ;
  64. echo $httpClient->request; // Get request header information
  65. echo $httpClient->response; // Get response header information
  66. echo $httpClient->buffer; // Get web page content
  67. $httpClient->get('http://accounts.haowei.me/ServiceLogin/', 'hash='. $time, 'uid=1;users=admin;')
  68. echo $httpClient->buffer ;
Copy code

php, httpClient


Statement:
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