php的httpClient 類

來源:互聯網
上載者:User
  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
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.