HasHttpRequest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * (c) overtrue <i@overtrue.me>
  4. *
  5. * Modified By yansongda <me@yansongda.cn>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Yansongda\Pay\Traits;
  11. use GuzzleHttp\Client;
  12. use Psr\Http\Message\ResponseInterface;
  13. trait HasHttpRequest
  14. {
  15. /**
  16. * Make a get request.
  17. *
  18. * @param string $endpoint
  19. * @param array $query
  20. * @param array $headers
  21. *
  22. * @return array
  23. */
  24. protected function get($endpoint, $query = [], $headers = [])
  25. {
  26. return $this->request('get', $endpoint, [
  27. 'headers' => $headers,
  28. 'query' => $query,
  29. ]);
  30. }
  31. /**
  32. * Make a post request.
  33. *
  34. * @param string $endpoint
  35. * @param mixed $params
  36. * @param array $options
  37. *
  38. * @return string
  39. */
  40. protected function post($endpoint, $params = [], ...$options)
  41. {
  42. $options = isset($options[0]) ? $options[0] : [];
  43. if (!is_array($params)) {
  44. $options['body'] = $params;
  45. } else {
  46. $options['form_params'] = $params;
  47. }
  48. return $this->request('post', $endpoint, $options);
  49. }
  50. /**
  51. * Make a http request.
  52. *
  53. * @param string $method
  54. * @param string $endpoint
  55. * @param array $options http://docs.guzzlephp.org/en/latest/request-options.html
  56. *
  57. * @return array
  58. */
  59. protected function request($method, $endpoint, $options = [])
  60. {
  61. return $this->unwrapResponse($this->getHttpClient($this->getBaseOptions())->{$method}($endpoint, $options));
  62. }
  63. /**
  64. * Return base Guzzle options.
  65. *
  66. * @return array
  67. */
  68. protected function getBaseOptions()
  69. {
  70. $options = [
  71. 'base_uri' => method_exists($this, 'getBaseUri') ? $this->getBaseUri() : '',
  72. 'timeout' => property_exists($this, 'timeout') ? $this->timeout : 5.0,
  73. ];
  74. return $options;
  75. }
  76. /**
  77. * Return http client.
  78. *
  79. * @param array $options
  80. *
  81. * @return \GuzzleHttp\Client
  82. */
  83. protected function getHttpClient(array $options = [])
  84. {
  85. return new Client($options);
  86. }
  87. /**
  88. * Convert response contents to json.
  89. *
  90. * @param \Psr\Http\Message\ResponseInterface $response
  91. *
  92. * @return array
  93. */
  94. protected function unwrapResponse(ResponseInterface $response)
  95. {
  96. $contentType = $response->getHeaderLine('Content-Type');
  97. $contents = $response->getBody()->getContents();
  98. if (false !== stripos($contentType, 'json') || stripos($contentType, 'javascript')) {
  99. return json_decode($contents, true);
  100. } elseif (false !== stripos($contentType, 'xml')) {
  101. return json_decode(json_encode(simplexml_load_string($contents)), true);
  102. }
  103. return $contents;
  104. }
  105. }