You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.3 KiB

2 years ago
  1. # GHttp
  2. 基于GuzzleHttp的简单版Http客户端。 Simple Http client base on GuzzleHttp
  3. ## 安装
  4. ```
  5. composer require jaeger/g-http
  6. ```
  7. ## 用法
  8. #### 1. get / getJson
  9. ```php
  10. use Jaeger\GHttp;
  11. $rt = GHttp::get('https://www.baidu.com/s?wd=QueryList');
  12. $rt = GHttp::get('https://www.baidu.com/s','wd=QueryList&wd2=teststr');
  13. //or
  14. $rt = GHttp::get('https://www.baidu.com/s',[
  15. 'wd' => 'QueryList',
  16. 'wd2' => 'teststr'
  17. ]);
  18. //opt
  19. $rt = GHttp::get('https://www.baidu.com/s',[
  20. 'wd' => 'QueryList'
  21. ],[
  22. 'headers' => [
  23. 'referer' => 'https://baidu.com',
  24. 'User-Agent' => 'Mozilla/5.0 (Windows NTChrome/58.0.3029.110 Safari/537.36',
  25. 'Cookie' => 'cookie xxx'
  26. ]
  27. ]);
  28. $rt = GHttp::getJson('https://xxxx.com/json');
  29. ```
  30. #### 2.post / postRaw / postJson
  31. ```php
  32. $rt = GHttp::post('https://www.posttestserver.com/post.php',[
  33. 'name' => 'QueryList',
  34. 'password' => 'ql'
  35. ]);
  36. $rt = GHttp::post('https://www.posttestserver.com/post.php','name=QueryList&password=ql');
  37. $rt = GHttp::postRaw('http://httpbin.org/post','raw data');
  38. $rt = GHttp::postRaw('http://httpbin.org/post',['aa' => 11,'bb' => 22]);
  39. $rt = GHttp::postJson('http://httpbin.org/post',['aa' => 11,'bb' => 22]);
  40. $rt = GHttp::postJson('http://httpbin.org/post','aa=11&bb=22');
  41. ```
  42. #### 3.download
  43. ```php
  44. GHttp::download('http://sw.bos.baidu.com/setup.exe','./path/to/xx.exe');
  45. ```
  46. ### 4. concurrent requests
  47. ```php
  48. use Jaeger\GHttp;
  49. $urls = [
  50. 'http://httpbin.org/get?name=php',
  51. 'http://httpbin.org/get?name=go',
  52. 'http://httpbin.org/get?name=c#',
  53. 'http://httpbin.org/get?name=java'
  54. ];
  55. GHttp::multiRequest($urls)->withHeaders([
  56. 'X-Powered-By' => 'Jaeger'
  57. ])->withOptions([
  58. 'timeout' => 10
  59. ])->concurrency(2)->success(function($response,$index){
  60. print_r((String)$response->getBody());
  61. print_r($index);
  62. })->error(function($reason,$index){
  63. print_r($reason);
  64. })->get();
  65. ```
  66. ```php
  67. use Jaeger\GHttp;
  68. use GuzzleHttp\Psr7\Request;
  69. $requests = [
  70. new Request('POST','http://httpbin.org/post',[
  71. 'Content-Type' => 'application/x-www-form-urlencoded',
  72. 'User-Agent' => 'g-http'
  73. ],http_build_query([
  74. 'name' => 'php'
  75. ])),
  76. new Request('POST','http://httpbin.org/post',[
  77. 'Content-Type' => 'application/x-www-form-urlencoded',
  78. 'User-Agent' => 'g-http'
  79. ],http_build_query([
  80. 'name' => 'go'
  81. ])),
  82. new Request('POST','http://httpbin.org/post',[
  83. 'Content-Type' => 'application/x-www-form-urlencoded',
  84. 'User-Agent' => 'g-http'
  85. ],http_build_query([
  86. 'name' => 'c#'
  87. ]))
  88. ];
  89. GHttp::multiRequest($requests)->success(function($response,$index){
  90. print_r((String)$response->getBody());
  91. print_r($index);
  92. })->post();
  93. ```
  94. ### 5. Request with cache
  95. Base on PHP-Cache: http://www.php-cache.com
  96. - Use filesystem cache
  97. ```php
  98. use Jaeger\GHttp;
  99. $rt = GHttp::get('http://httpbin.org/get',[
  100. 'wd' => 'QueryList'
  101. ],[
  102. 'cache' => __DIR__,
  103. 'cache_ttl' => 120 //seconds
  104. ]);
  105. ```
  106. - Use predis cache
  107. Install predis adapter:
  108. ```
  109. composer require cache/predis-adapter
  110. ```
  111. Usage:
  112. ```php
  113. use Jaeger\GHttp;
  114. use Cache\Adapter\Predis\PredisCachePool;
  115. $client = new \Predis\Client('tcp:/127.0.0.1:6379');
  116. $pool = new PredisCachePool($client);
  117. $rt = GHttp::get('http://httpbin.org/get',[
  118. 'wd' => 'QueryList'
  119. ],[
  120. 'cache' => $pool,
  121. 'cache_ttl' => 120 //seconds
  122. ]);
  123. ```