GHttp.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * 基于GuzzleHttp的简单版Http客户端。 Simple Http client base on GuzzleHttp
  4. *
  5. * @Author: Jaeger <JaegerCode@gmail.com>
  6. *
  7. * @Version V1.0
  8. */
  9. namespace Jaeger;
  10. use GuzzleHttp\Client;
  11. /**
  12. * Class GHttp
  13. * @package Jaeger
  14. *
  15. * @method static string get($url,$args = null,$otherArgs = [])
  16. * @method static mixed getJson($url, $args = null, $otherArgs = [])
  17. * @method static string post($url,$args = null,$otherArgs = [])
  18. * @method static string postRaw($url, $raw = null, $otherArgs = [])
  19. * @method static string postJson($url, $args = null, $otherArgs = [])
  20. */
  21. class GHttp
  22. {
  23. private static $client = null;
  24. public static function __callStatic($name, $arguments)
  25. {
  26. $protectedName = '_'.$name;
  27. if(method_exists(self::class,$protectedName)){
  28. return Cache::remember($protectedName, $arguments);
  29. }
  30. throw new MethodNotFoundException('Call undefined method '.self::class.':'.$name.'()');
  31. }
  32. public static function getClient(array $config = [])
  33. {
  34. if(self::$client == null){
  35. self::$client = new Client($config);
  36. }
  37. return self::$client;
  38. }
  39. /**
  40. * @param $url
  41. * @param array $args
  42. * @param array $otherArgs
  43. * @return string
  44. */
  45. protected static function _get($url,$args = null,$otherArgs = [])
  46. {
  47. is_string($args) && parse_str($args,$args);
  48. $args = array_merge([
  49. 'verify' => false,
  50. 'query' => $args,
  51. 'headers' => [
  52. 'referer' => $url,
  53. 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
  54. ]
  55. ],$otherArgs);
  56. $client = self::getClient();
  57. $response = $client->request('GET', $url,$args);
  58. return (string)$response->getBody();
  59. }
  60. protected static function _getJson($url, $args = null, $otherArgs = [])
  61. {
  62. $data = self::get($url, $args , $otherArgs);
  63. return json_decode($data,JSON_UNESCAPED_UNICODE);
  64. }
  65. /**
  66. * @param $url
  67. * @param array $args
  68. * @param array $otherArgs
  69. * @return string
  70. */
  71. protected static function _post($url,$args = null,$otherArgs = [])
  72. {
  73. is_string($args) && parse_str($args,$args);
  74. $args = array_merge([
  75. 'verify' => false,
  76. 'form_params' => $args,
  77. 'headers' => [
  78. 'referer' => $url,
  79. 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
  80. ]
  81. ],$otherArgs);
  82. $client = self::getClient();
  83. $response = $client->request('Post', $url,$args);
  84. return (string)$response->getBody();
  85. }
  86. /**
  87. * @param $url
  88. * @param null $raw
  89. * @param array $otherArgs
  90. * @return string
  91. */
  92. protected static function _postRaw($url, $raw = null, $otherArgs = [])
  93. {
  94. is_array($raw) && $raw = json_encode($raw);
  95. $args = array_merge([
  96. 'verify' => false,
  97. 'body' => $raw,
  98. 'headers' => [
  99. 'referer' => $url,
  100. 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
  101. ]
  102. ],$otherArgs);
  103. $client = self::getClient();
  104. $response = $client->request('Post', $url,$args);
  105. return (string)$response->getBody();
  106. }
  107. /**
  108. * @param $url
  109. * @param null $args
  110. * @param array $otherArgs
  111. * @return string
  112. */
  113. protected static function _postJson($url, $args = null, $otherArgs = [])
  114. {
  115. is_string($args) && parse_str($args,$args);
  116. $args = array_merge([
  117. 'verify' => false,
  118. 'json' => $args,
  119. 'headers' => [
  120. 'referer' => $url,
  121. 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
  122. ]
  123. ],$otherArgs);
  124. $client = self::getClient();
  125. $response = $client->request('Post', $url,$args);
  126. return (string)$response->getBody();
  127. }
  128. /**
  129. * @param $url
  130. * @param $filePath
  131. * @param null $args
  132. * @param array $otherArgs
  133. * @return string
  134. */
  135. public static function download($url,$filePath,$args = null,$otherArgs = [])
  136. {
  137. $otherArgs = array_merge($otherArgs,[
  138. 'sink' => $filePath,
  139. ]);
  140. return self::get($url,$args,$otherArgs);
  141. }
  142. /**
  143. * @param $urls
  144. * @return MultiRequest
  145. */
  146. public static function multiRequest($urls)
  147. {
  148. $client = self::getClient();
  149. return MultiRequest::newRequest($client)->urls($urls);
  150. }
  151. }