Client.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. /**
  3. * A parallel HTTP client written in pure PHP
  4. *
  5. * @author hightman <hightman@twomice.net>
  6. * @link http://hightman.cn
  7. * @copyright Copyright (c) 2015 Twomice Studio.
  8. */
  9. namespace hightman\http;
  10. /**
  11. * Http client
  12. *
  13. * @author hightman
  14. * @since 1.0
  15. */
  16. class Client
  17. {
  18. use HeaderTrait;
  19. const PACKAGE = __CLASS__;
  20. const VERSION = '1.0.0-beta';
  21. const CRLF = "\r\n";
  22. private $_cookiePath, $_parser, $_timeout;
  23. private static $_debugOpen = false;
  24. private static $_processKey;
  25. /**
  26. * Open/close debug mode
  27. * @param string $msg
  28. */
  29. public static function debug($msg)
  30. {
  31. if ($msg === 'open' || $msg === 'close') {
  32. self::$_debugOpen = $msg === 'open';
  33. } elseif (self::$_debugOpen === true) {
  34. $key = self::$_processKey === null ? '' : '[' . self::$_processKey . '] ';
  35. echo '[DEBUG] ', date('H:i:s '), $key, implode('', func_get_args()), self::CRLF;
  36. }
  37. }
  38. /**
  39. * Decompress data
  40. * @param string $data compressed string
  41. * @return string result string
  42. */
  43. public static function gzdecode($data)
  44. {
  45. return gzinflate(substr($data, 10, -8));
  46. }
  47. /**
  48. * Constructor
  49. * @param callable $p response parse handler
  50. */
  51. public function __construct($p = null)
  52. {
  53. $this->applyDefaultHeader();
  54. $this->setParser($p);
  55. }
  56. /**
  57. * Destructor
  58. * Export and save all cookies.
  59. */
  60. public function __destruct()
  61. {
  62. if ($this->_cookiePath !== null) {
  63. $this->saveCookie($this->_cookiePath);
  64. }
  65. }
  66. /**
  67. * Set the max network read timeout
  68. * @param float $sec seconds, decimal support
  69. */
  70. public function setTimeout($sec)
  71. {
  72. $this->_timeout = floatval($sec);
  73. }
  74. /**
  75. * Set cookie storage path
  76. * If set, all cookies will be saved into this file, and send to request on need.
  77. * @param string $file file path to store cookies.
  78. */
  79. public function setCookiePath($file)
  80. {
  81. $this->_cookiePath = $file;
  82. $this->loadCookie($file);
  83. }
  84. /**
  85. * Set response parse handler
  86. * @param callable $p parse handler
  87. */
  88. public function setParser($p)
  89. {
  90. if ($p === null || $p instanceof ParseInterface || is_callable($p)) {
  91. $this->_parser = $p;
  92. }
  93. }
  94. /**
  95. * Run parse handler
  96. * @param Response $res response object
  97. * @param Request $req request object
  98. * @param mixed $key the key string of multi request
  99. */
  100. public function runParser($res, $req, $key = null)
  101. {
  102. if ($this->_parser !== null) {
  103. self::debug('run parser: ', $req->getRawUrl());
  104. if ($this->_parser instanceof ParseInterface) {
  105. $this->_parser->parse($res, $req, $key);
  106. } else {
  107. call_user_func($this->_parser, $res, $req, $key);
  108. }
  109. }
  110. }
  111. /**
  112. * Clear headers and apply defaults.
  113. */
  114. public function clearHeader()
  115. {
  116. parent::clearHeader();
  117. $this->applyDefaultHeader();
  118. }
  119. /**
  120. * Shortcut of HEAD request
  121. * @param string $url request URL string.
  122. * @param array $params query params appended to URL.
  123. * @return Response result response object.
  124. */
  125. public function head($url, $params = [])
  126. {
  127. if (is_array($url)) {
  128. return $this->mhead($url, $params);
  129. }
  130. return $this->exec($this->buildRequest('HEAD', $url, $params));
  131. }
  132. /**
  133. * Shortcut of HEAD multiple requests in parallel
  134. * @param array $urls request URL list.
  135. * @param array $params query params appended to each URL.
  136. * @return Response[] result response objects associated with key of URL.
  137. */
  138. public function mhead($urls, $params = [])
  139. {
  140. return $this->exec($this->buildRequests('HEAD', $urls, $params));
  141. }
  142. /**
  143. * Shortcut of GET request
  144. * @param string $url request URL string.
  145. * @param array $params extra query params, appended to URL.
  146. * @return Response result response object.
  147. */
  148. public function get($url, $params = [])
  149. {
  150. if (is_array($url)) {
  151. return $this->mget($url, $params);
  152. }
  153. return $this->exec($this->buildRequest('GET', $url, $params));
  154. }
  155. /**
  156. * Shortcut of GET multiple requests in parallel
  157. * @param array $urls request URL list.
  158. * @param array $params query params appended to each URL.
  159. * @return Response[] result response objects associated with key of URL.
  160. */
  161. public function mget($urls, $params = [])
  162. {
  163. return $this->exec($this->buildRequests('GET', $urls, $params));
  164. }
  165. /**
  166. * Shortcut of DELETE request
  167. * @param string $url request URL string.
  168. * @param array $params extra query params, appended to URL.
  169. * @return Response result response object.
  170. */
  171. public function delete($url, $params = [])
  172. {
  173. if (is_array($url)) {
  174. return $this->mdelete($url, $params);
  175. }
  176. return $this->exec($this->buildRequest('DELETE', $url, $params));
  177. }
  178. /**
  179. * Shortcut of DELETE multiple requests in parallel
  180. * @param array $urls request URL list.
  181. * @param array $params query params appended to each URL.
  182. * @return Response[] result response objects associated with key of URL.
  183. */
  184. public function mdelete($urls, $params = [])
  185. {
  186. return $this->exec($this->buildRequests('DELETE', $urls, $params));
  187. }
  188. /**
  189. * Shortcut of POST request
  190. * @param string|Request $url request URL string, or request object.
  191. * @param array $params post fields.
  192. * @return Response result response object.
  193. */
  194. public function post($url, $params = [])
  195. {
  196. $req = $url instanceof Request ? $url : $this->buildRequest('POST', $url);
  197. foreach ($params as $key => $value) {
  198. $req->addPostField($key, $value);
  199. }
  200. return $this->exec($req);
  201. }
  202. /**
  203. * Shortcut of PUT request
  204. * @param string|Request $url request URL string, or request object.
  205. * @param string $content content to be put.
  206. * @return Response result response object.
  207. */
  208. public function put($url, $content = '')
  209. {
  210. $req = $url instanceof Request ? $url : $this->buildRequest('PUT', $url);
  211. $req->setBody($content);
  212. return $this->exec($req);
  213. }
  214. /**
  215. * Shortcut of GET restful request as json format
  216. * @param string $url request URL string.
  217. * @param array $params extra query params, appended to URL.
  218. * @return array result json data, or false on failure.
  219. */
  220. public function getJson($url, $params = [])
  221. {
  222. $req = $this->buildRequest('GET', $url, $params);
  223. $req->setHeader('accept', 'application/json');
  224. $res = $this->exec($req);
  225. return $res === false ? false : $res->getJson();
  226. }
  227. /**
  228. * Shortcut of POST restful request as json format
  229. * @param string $url request URL string.
  230. * @param array $params request json data to be post.
  231. * @return array result json data, or false on failure.
  232. */
  233. public function postJson($url, $params = [])
  234. {
  235. $req = $this->buildRequest('POST', $url);
  236. $req->setHeader('accept', 'application/json');
  237. $req->setJsonBody($params);
  238. $res = $this->exec($req);
  239. return $res === false ? false : $res->getJson();
  240. }
  241. /**
  242. * Shortcut of PUT restful request as json format
  243. * @param string $url request URL string.
  244. * @param array $params request json data to be put.
  245. * @return array result json data, or false on failure.
  246. */
  247. public function putJson($url, $params = [])
  248. {
  249. $req = $this->buildRequest('PUT', $url);
  250. $req->setHeader('accept', 'application/json');
  251. $req->setJsonBody($params);
  252. $res = $this->exec($req);
  253. return $res === false ? false : $res->getJson();
  254. }
  255. /**
  256. * Execute http requests
  257. * @param Request|Request[] $req the request object, or array of multiple requests
  258. * @return Response|Response[] result response object, or response array for multiple requests
  259. */
  260. public function exec($req)
  261. {
  262. // build recs
  263. $recs = [];
  264. if ($req instanceof Request) {
  265. $recs[] = new Processor($this, $req);
  266. } elseif (is_array($req)) {
  267. foreach ($req as $key => $value) {
  268. if ($value instanceof Request) {
  269. $recs[$key] = new Processor($this, $value, $key);
  270. }
  271. }
  272. }
  273. if (count($recs) === 0) {
  274. return false;
  275. }
  276. // loop to process
  277. while (true) {
  278. // build select fds
  279. $rfds = $wfds = $xrec = [];
  280. $xfds = null;
  281. foreach ($recs as $rec) {
  282. /* @var $rec Processor */
  283. self::$_processKey = $rec->key;
  284. if ($rec->finished || !($conn = $rec->getConn())) {
  285. continue;
  286. }
  287. if ($this->_timeout !== null) {
  288. $xrec[] = $rec;
  289. }
  290. $rfds[] = $conn->getSock();
  291. if ($conn->hasDataToWrite()) {
  292. $wfds[] = $conn->getSock();
  293. }
  294. }
  295. self::$_processKey = null;
  296. if (count($rfds) === 0 && count($wfds) === 0) {
  297. // all tasks finished
  298. break;
  299. }
  300. // select sockets
  301. self::debug('stream_select(rfds[', count($rfds), '], wfds[', count($wfds), ']) ...');
  302. if ($this->_timeout === null) {
  303. $num = stream_select($rfds, $wfds, $xfds, null);
  304. } else {
  305. $sec = intval($this->_timeout);
  306. $usec = intval(($this->_timeout - $sec) * 1000000);
  307. $num = stream_select($rfds, $wfds, $xfds, $sec, $usec);
  308. }
  309. self::debug('select result: ', $num === false ? 'false' : $num);
  310. if ($num === false) {
  311. trigger_error('stream_select() error', E_USER_WARNING);
  312. break;
  313. } elseif ($num > 0) {
  314. // wfds
  315. foreach ($wfds as $sock) {
  316. if (!($conn = Connection::findBySock($sock))) {
  317. continue;
  318. }
  319. $rec = $conn->getExArg();
  320. /* @var $rec Processor */
  321. self::$_processKey = $rec->key;
  322. $rec->send();
  323. }
  324. // rfds
  325. foreach ($rfds as $sock) {
  326. if (!($conn = Connection::findBySock($sock))) {
  327. continue;
  328. }
  329. $rec = $conn->getExArg();
  330. /* @var $rec Processor */
  331. self::$_processKey = $rec->key;
  332. $rec->recv();
  333. }
  334. } else {
  335. // force to close request
  336. foreach ($xrec as $rec) {
  337. self::$_processKey = $rec->key;
  338. $rec->finish('TIMEOUT');
  339. }
  340. }
  341. }
  342. // return value
  343. if (!is_array($req)) {
  344. $ret = $recs[0]->res;
  345. } else {
  346. $ret = [];
  347. foreach ($recs as $key => $rec) {
  348. $ret[$key] = $rec->res;
  349. }
  350. }
  351. return $ret;
  352. }
  353. /**
  354. * Build a http request
  355. * @param string $method
  356. * @param string $url
  357. * @param array $params
  358. * @return Request
  359. */
  360. protected function buildRequest($method, $url, $params = [])
  361. {
  362. if (count($params) > 0) {
  363. $url .= strpos($url, '?') === false ? '?' : '&';
  364. $url .= http_build_query($params);
  365. }
  366. return new Request($url, $method);
  367. }
  368. /**
  369. * Build multiple http requests
  370. * @param string $method
  371. * @param array $urls
  372. * @param array $params
  373. * @return Request[]
  374. */
  375. protected function buildRequests($method, $urls, $params = [])
  376. {
  377. $reqs = [];
  378. foreach ($urls as $key => $url) {
  379. $reqs[$key] = $this->buildRequest($method, $url, $params);
  380. }
  381. return $reqs;
  382. }
  383. /**
  384. * @return string default user-agent
  385. */
  386. protected function defaultAgent()
  387. {
  388. $agent = 'Mozilla/5.0 (Compatible; ' . self::PACKAGE . '/' . self::VERSION . ') ';
  389. $agent .= 'php-' . php_sapi_name() . '/' . phpversion() . ' ';
  390. $agent .= php_uname('s') . '/' . php_uname('r');
  391. return $agent;
  392. }
  393. /**
  394. * Default HTTP headers
  395. */
  396. protected function applyDefaultHeader()
  397. {
  398. $this->setHeader([
  399. 'accept' => '*/*',
  400. 'accept-language' => 'zh-cn,zh',
  401. 'connection' => 'Keep-Alive',
  402. 'user-agent' => $this->defaultAgent(),
  403. ]);
  404. }
  405. }