HttpClient.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace crmeb\services;
  3. class HttpClient {
  4. public $http_code;
  5. public $url;
  6. public $host = "http://202.104.113.60";
  7. public $timeout = 30;
  8. public $connectTimeout = 30;
  9. public $decode_json = TRUE;
  10. public $http_info;
  11. public $http_header;
  12. public $debug = FALSE;
  13. public $debugInfo = array();
  14. public $errorInfo = '';
  15. function __construct($host = null, $port = 80) {
  16. $this->host = $host;
  17. }
  18. public function getErrorInfo() {
  19. return $this->errorInfo;
  20. }
  21. public function getHttpCode() {
  22. if ($this->http_info) {
  23. return $this->http_info['http_code'];
  24. }
  25. return "unknow";
  26. }
  27. function setHost($host = null, $port = 80) {
  28. $this->host = $host;
  29. $this->port = $port;
  30. }
  31. function setDebug($debug) {
  32. $this->debug = $debug;
  33. }
  34. function get($url, $parameters = array(), $headers = array()) {
  35. $response = $this->oAuthRequest($url, 'GET', $parameters, $headers);
  36. if ($this->decode_json) {
  37. return json_decode($response, true);
  38. }
  39. return $response;
  40. }
  41. function post($url, $parameters = array(), $headers = array()) {
  42. $response = $this->oAuthRequest($url, 'POST', $parameters, $headers);
  43. if ($this->decode_json) {
  44. return json_decode($response, true);
  45. }
  46. return $response;
  47. }
  48. function delete($url, $parameters = array(), $headers = array()) {
  49. $response = $this->oAuthRequest($url, 'DELETE', $parameters);
  50. if ($this->decode_json) {
  51. return json_decode($response, true);
  52. }
  53. return $response;
  54. }
  55. function oAuthRequest($url, $method, $parameters, $headers = array()) {
  56. /* if (strrpos($url, 'http://') !== 0 && strrpos($url, 'https://') !== 0) {
  57. $url = "{$this->host}{$url}";
  58. } */
  59. switch ($method) {
  60. case 'GET':
  61. $url = $url . '?' . http_build_query($parameters);
  62. return $this->http($url, 'GET');
  63. default:
  64. if (is_array($parameters) || is_object($parameters)) {
  65. $parameters = http_build_query($parameters);
  66. }
  67. return $this->http($url, $method, $parameters, $headers);
  68. }
  69. }
  70. function http($url, $method, $postfields = NULL, $headers = array()) {
  71. $this->http_info = array();
  72. $ssl = substr($url, 0, 8) == "https://" ? true : false;
  73. $ci = curl_init();
  74. curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  75. //curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
  76. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
  77. curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
  78. curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
  79. curl_setopt($ci, CURLOPT_ENCODING, "");
  80. if ($ssl) {
  81. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
  82. curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false); // 检查证书中是否设置域名
  83. }
  84. curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:')); //避免data数据过长问题
  85. curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
  86. curl_setopt($ci, CURLOPT_HEADER, FALSE);
  87. switch ($method) {
  88. case 'POST':
  89. curl_setopt($ci, CURLOPT_POST, TRUE);
  90. if (!empty($postfields)) {
  91. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  92. $this->postdata = $postfields;
  93. }
  94. break;
  95. case 'DELETE':
  96. curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
  97. if (!empty($postfields)) {
  98. $url = "{$url}?{$postfields}";
  99. }
  100. }
  101. curl_setopt($ci, CURLOPT_URL, $url);
  102. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  103. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
  104. $response = curl_exec($ci);
  105. $this->http_info = curl_getinfo($ci);
  106. $this->url = $url;
  107. if ($this->debug) {
  108. //$this->debugInfo[] = "=====post data======";
  109. //$this->debugInfo[] = $postfields;
  110. $this->debugInfo[] = "=====headers======";
  111. $this->debugInfo[] = $headers;
  112. $this->debugInfo[] = '=====request info=====' . "";
  113. $this->debugInfo[] = var_export(curl_getinfo($ci), true);
  114. $this->debugInfo[] = '=====response=====' . "\r\n";
  115. $this->debugInfo[] = $response;
  116. }
  117. $er = curl_error($ci);
  118. curl_close($ci);
  119. if ($er) {
  120. return $this->errorInfo = "Curl error:" . $er;
  121. }
  122. return $response;
  123. }
  124. function getHeader($ch, $header) {
  125. $i = strpos($header, ':');
  126. if (!empty($i)) {
  127. $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
  128. $value = trim(substr($header, $i + 2));
  129. $this->http_header[$key] = $value;
  130. }
  131. return strlen($header);
  132. }
  133. public function getDebugInfo() {
  134. return $this->debugInfo;
  135. }
  136. }
  137. ?>