AipHttpClient.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /**
  18. * Http Client
  19. */
  20. class AipHttpClient{
  21. /**
  22. * HttpClient
  23. * @param array $headers HTTP header
  24. */
  25. public function __construct($headers=array()){
  26. $this->headers = $this->buildHeaders($headers);
  27. $this->connectTimeout = 60000;
  28. $this->socketTimeout = 60000;
  29. }
  30. /**
  31. * 连接超时
  32. * @param int $ms 毫秒
  33. */
  34. public function setConnectionTimeoutInMillis($ms){
  35. $this->connectTimeout = $ms;
  36. }
  37. /**
  38. * 响应超时
  39. * @param int $ms 毫秒
  40. */
  41. public function setSocketTimeoutInMillis($ms){
  42. $this->socketTimeout = $ms;
  43. }
  44. /**
  45. * @param string $url
  46. * @param array $data HTTP POST BODY
  47. * @param array $param HTTP URL
  48. * @param array $headers HTTP header
  49. * @return array
  50. */
  51. public function post($url, $data=array(), $params=array(), $headers=array()){
  52. $url = $this->buildUrl($url, $params);
  53. $headers = array_merge($this->headers, $this->buildHeaders($headers));
  54. $ch = curl_init();
  55. curl_setopt($ch, CURLOPT_URL, $url);
  56. curl_setopt($ch, CURLOPT_POST, 1);
  57. curl_setopt($ch, CURLOPT_HEADER, false);
  58. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  59. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  60. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  61. curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data);
  62. curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->socketTimeout);
  63. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connectTimeout);
  64. $content = curl_exec($ch);
  65. $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  66. if($code === 0){
  67. throw new Exception(curl_error($ch));
  68. }
  69. curl_close($ch);
  70. return array(
  71. 'code' => $code,
  72. 'content' => $content,
  73. );
  74. }
  75. /**
  76. * @param string $url
  77. * @param array $datas HTTP POST BODY
  78. * @param array $param HTTP URL
  79. * @param array $headers HTTP header
  80. * @return array
  81. */
  82. public function multi_post($url, $datas=array(), $params=array(), $headers=array()){
  83. $url = $this->buildUrl($url, $params);
  84. $headers = array_merge($this->headers, $this->buildHeaders($headers));
  85. $chs = array();
  86. $result = array();
  87. $mh = curl_multi_init();
  88. foreach($datas as $data){
  89. $ch = curl_init();
  90. $chs[] = $ch;
  91. curl_setopt($ch, CURLOPT_URL, $url);
  92. curl_setopt($ch, CURLOPT_POST, 1);
  93. curl_setopt($ch, CURLOPT_HEADER, false);
  94. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  95. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  96. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  97. curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data);
  98. curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->socketTimeout);
  99. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connectTimeout);
  100. curl_multi_add_handle($mh, $ch);
  101. }
  102. $running = null;
  103. do{
  104. curl_multi_exec($mh, $running);
  105. usleep(100);
  106. }while($running);
  107. foreach($chs as $ch){
  108. $content = curl_multi_getcontent($ch);
  109. $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  110. $result[] = array(
  111. 'code' => $code,
  112. 'content' => $content,
  113. );
  114. curl_multi_remove_handle($mh, $ch);
  115. }
  116. curl_multi_close($mh);
  117. return $result;
  118. }
  119. /**
  120. * @param string $url
  121. * @param array $param HTTP URL
  122. * @param array $headers HTTP header
  123. * @return array
  124. */
  125. public function get($url, $params=array(), $headers=array()){
  126. $url = $this->buildUrl($url, $params);
  127. $headers = array_merge($this->headers, $this->buildHeaders($headers));
  128. $ch = curl_init();
  129. curl_setopt($ch, CURLOPT_URL, $url);
  130. curl_setopt($ch, CURLOPT_HEADER, false);
  131. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  132. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  133. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  134. curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->socketTimeout);
  135. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connectTimeout);
  136. $content = curl_exec($ch);
  137. $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  138. if($code === 0){
  139. throw new Exception(curl_error($ch));
  140. }
  141. curl_close($ch);
  142. return array(
  143. 'code' => $code,
  144. 'content' => $content,
  145. );
  146. }
  147. /**
  148. * 构造 header
  149. * @param array $headers
  150. * @return array
  151. */
  152. private function buildHeaders($headers){
  153. $result = array();
  154. foreach($headers as $k => $v){
  155. $result[] = sprintf('%s:%s', $k, $v);
  156. }
  157. return $result;
  158. }
  159. /**
  160. *
  161. * @param string $url
  162. * @param array $params 参数
  163. * @return string
  164. */
  165. private function buildUrl($url, $params){
  166. if(!empty($params)){
  167. $str = http_build_query($params);
  168. return $url . (strpos($url, '?') === false ? '?' : '&') . $str;
  169. }else{
  170. return $url;
  171. }
  172. }
  173. }