AipHttpClient.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace addons\blog\library\aip\lib;
  3. /*
  4. * Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. * use this file except in compliance with the License. You may obtain a copy of
  8. * the License at
  9. *
  10. * Http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. * License for the specific language governing permissions and limitations under
  16. * the License.
  17. */
  18. /**
  19. * Http Client
  20. */
  21. class AipHttpClient
  22. {
  23. /**
  24. * HttpClient
  25. * @param array $headers HTTP header
  26. */
  27. public function __construct($headers = array())
  28. {
  29. $this->headers = $this->buildHeaders($headers);
  30. $this->connectTimeout = 60000;
  31. $this->socketTimeout = 60000;
  32. $this->conf = array();
  33. }
  34. /**
  35. * 连接超时
  36. * @param int $ms 毫秒
  37. */
  38. public function setConnectionTimeoutInMillis($ms)
  39. {
  40. $this->connectTimeout = $ms;
  41. }
  42. /**
  43. * 响应超时
  44. * @param int $ms 毫秒
  45. */
  46. public function setSocketTimeoutInMillis($ms)
  47. {
  48. $this->socketTimeout = $ms;
  49. }
  50. /**
  51. * 配置
  52. * @param array $conf
  53. */
  54. public function setConf($conf)
  55. {
  56. $this->conf = $conf;
  57. }
  58. /**
  59. * 请求预处理
  60. * @param resource $ch
  61. */
  62. public function prepare($ch)
  63. {
  64. foreach ($this->conf as $key => $value) {
  65. curl_setopt($ch, $key, $value);
  66. }
  67. }
  68. /**
  69. * @param string $url
  70. * @param array $data HTTP POST BODY
  71. * @param array $param HTTP URL
  72. * @param array $headers HTTP header
  73. * @return array
  74. */
  75. public function post($url, $data = array(), $params = array(), $headers = array())
  76. {
  77. $url = $this->buildUrl($url, $params);
  78. $headers = array_merge($this->headers, $this->buildHeaders($headers));
  79. $ch = curl_init();
  80. $this->prepare($ch);
  81. curl_setopt($ch, CURLOPT_URL, $url);
  82. curl_setopt($ch, CURLOPT_POST, 1);
  83. curl_setopt($ch, CURLOPT_HEADER, false);
  84. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  85. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  86. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  87. curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data);
  88. curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->socketTimeout);
  89. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connectTimeout);
  90. $content = curl_exec($ch);
  91. $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  92. if ($code === 0) {
  93. throw new Exception(curl_error($ch));
  94. }
  95. curl_close($ch);
  96. return array(
  97. 'code' => $code,
  98. 'content' => $content,
  99. );
  100. }
  101. /**
  102. * @param string $url
  103. * @param array $datas HTTP POST BODY
  104. * @param array $param HTTP URL
  105. * @param array $headers HTTP header
  106. * @return array
  107. */
  108. public function multi_post($url, $datas = array(), $params = array(), $headers = array())
  109. {
  110. $url = $this->buildUrl($url, $params);
  111. $headers = array_merge($this->headers, $this->buildHeaders($headers));
  112. $chs = array();
  113. $result = array();
  114. $mh = curl_multi_init();
  115. foreach ($datas as $data) {
  116. $ch = curl_init();
  117. $chs[] = $ch;
  118. $this->prepare($ch);
  119. curl_setopt($ch, CURLOPT_URL, $url);
  120. curl_setopt($ch, CURLOPT_POST, 1);
  121. curl_setopt($ch, CURLOPT_HEADER, false);
  122. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  123. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  124. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  125. curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data);
  126. curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->socketTimeout);
  127. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connectTimeout);
  128. curl_multi_add_handle($mh, $ch);
  129. }
  130. $running = null;
  131. do {
  132. curl_multi_exec($mh, $running);
  133. usleep(100);
  134. } while ($running);
  135. foreach ($chs as $ch) {
  136. $content = curl_multi_getcontent($ch);
  137. $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  138. $result[] = array(
  139. 'code' => $code,
  140. 'content' => $content,
  141. );
  142. curl_multi_remove_handle($mh, $ch);
  143. }
  144. curl_multi_close($mh);
  145. return $result;
  146. }
  147. /**
  148. * @param string $url
  149. * @param array $param HTTP URL
  150. * @param array $headers HTTP header
  151. * @return array
  152. */
  153. public function get($url, $params = array(), $headers = array())
  154. {
  155. $url = $this->buildUrl($url, $params);
  156. $headers = array_merge($this->headers, $this->buildHeaders($headers));
  157. $ch = curl_init();
  158. $this->prepare($ch);
  159. curl_setopt($ch, CURLOPT_URL, $url);
  160. curl_setopt($ch, CURLOPT_HEADER, false);
  161. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  162. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  163. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  164. curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->socketTimeout);
  165. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connectTimeout);
  166. $content = curl_exec($ch);
  167. $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  168. if ($code === 0) {
  169. throw new Exception(curl_error($ch));
  170. }
  171. curl_close($ch);
  172. return array(
  173. 'code' => $code,
  174. 'content' => $content,
  175. );
  176. }
  177. /**
  178. * 构造 header
  179. * @param array $headers
  180. * @return array
  181. */
  182. private function buildHeaders($headers)
  183. {
  184. $result = array();
  185. foreach ($headers as $k => $v) {
  186. $result[] = sprintf('%s:%s', $k, $v);
  187. }
  188. return $result;
  189. }
  190. /**
  191. *
  192. * @param string $url
  193. * @param array $params 参数
  194. * @return string
  195. */
  196. private function buildUrl($url, $params)
  197. {
  198. if (!empty($params)) {
  199. $str = http_build_query($params);
  200. return $url . (strpos($url, '?') === false ? '?' : '&') . $str;
  201. } else {
  202. return $url;
  203. }
  204. }
  205. }