Heim  >  Artikel  >  Backend-Entwicklung  >  php的httpClient 类

php的httpClient 类

WBOY
WBOYOriginal
2016-07-25 08:43:311460Durchsuche
  1. class httpClient {
  2. public $buffer = null; // buffer 获取返回的字符串
  3. public $referer = null; // referer 设置 HTTP_REFERER 的网址
  4. public $response = null; // response 服务器响应的 header 信息
  5. public $request = null; // request 发送到服务器的 header 信息
  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; // 获取 请求头部信息
  65. echo $httpClient->response; // 获取 响应的头部信息
  66. echo $httpClient->buffer; // 获取 网页内容
  67. $httpClient->get('http://accounts.haowei.me/ServiceLogin/', 'hash='. $time, 'uid=1;users=admin;')
  68. echo $httpClient->buffer;
复制代码

php, httpClient


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:编程语言漫谈 Nächster Artikel:php在运行时动态创建函数