Response.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  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 App\Libs\TikTok\Kernel\Http;
  11. use App\Libs\TikTok\Kernel\Exceptions\HttpException;
  12. use App\Libs\TikTok\Kernel\Support\Collection;
  13. use GuzzleHttp\Psr7\Response as GuzzleResponse;
  14. use Psr\Http\Message\ResponseInterface;
  15. /**
  16. * Class Response.
  17. *
  18. * @author overtrue <i@overtrue.me>
  19. */
  20. class Response extends GuzzleResponse {
  21. /**
  22. * @return string
  23. */
  24. public function getBodyContents(): string {
  25. $this->getBody()->rewind();
  26. $contents = $this->getBody()->getContents();
  27. $this->getBody()->rewind();
  28. return $contents;
  29. }
  30. /**
  31. * @param ResponseInterface $response
  32. *
  33. * @return Response
  34. */
  35. public static function buildFromPsrResponse(ResponseInterface $response): Response {
  36. return new static(
  37. $response->getStatusCode(),
  38. $response->getHeaders(),
  39. $response->getBody(),
  40. $response->getProtocolVersion(),
  41. $response->getReasonPhrase()
  42. );
  43. }
  44. /**
  45. * Build to json.
  46. * @return string
  47. * @throws HttpException
  48. * @author zhaoxiang <zhaoxiang051405@gmail.com>
  49. */
  50. public function toJson(): string {
  51. return json_encode($this->toArray());
  52. }
  53. /**
  54. * Build to array.
  55. * @return array
  56. * @throws HttpException
  57. * @author zhaoxiang <zhaoxiang051405@gmail.com>
  58. */
  59. public function toArray(): array {
  60. $content = $this->removeControlCharacters($this->getBodyContents());
  61. if (false !== stripos($this->getHeaderLine('Content-Type'), 'xml') || 0 === stripos($content, '<xml')) {
  62. throw new HttpException('Http Error');
  63. }
  64. $array = json_decode($content, true, 512, JSON_BIGINT_AS_STRING);
  65. if (JSON_ERROR_NONE === json_last_error()) {
  66. return (array)$array;
  67. }
  68. return [];
  69. }
  70. /**
  71. * Get collection data.
  72. * @return Collection
  73. * @throws HttpException
  74. * @author zhaoxiang <zhaoxiang051405@gmail.com>
  75. */
  76. public function toCollection(): Collection {
  77. return new Collection($this->toArray());
  78. }
  79. /**
  80. *
  81. * @return object
  82. * @throws HttpException
  83. * @author zhaoxiang <zhaoxiang051405@gmail.com>
  84. */
  85. public function toObject(): object {
  86. return json_decode($this->toJson());
  87. }
  88. /**
  89. * @return string
  90. */
  91. public function __toString() {
  92. return $this->getBodyContents();
  93. }
  94. /**
  95. * @param string $content
  96. *
  97. * @return string
  98. */
  99. protected function removeControlCharacters(string $content): string {
  100. return \preg_replace('/[\x00-\x1F\x80-\x9F]/u', '', $content);
  101. }
  102. }