AipHttpClient.php 6.5 KB

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