HttpResponse.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils\Ocr\Http;
  8. /*
  9. * Licensed to the Apache Software Foundation (ASF) under one
  10. * or more contributor license agreements. See the NOTICE file
  11. * distributed with this work for additional information
  12. * regarding copyright ownership. The ASF licenses this file
  13. * to you under the Apache License, Version 2.0 (the
  14. * "License"); you may not use this file except in compliance
  15. * with the License. You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing,
  20. * software distributed under the License is distributed on an
  21. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  22. * KIND, either express or implied. See the License for the
  23. * specific language governing permissions and limitations
  24. * under the License.
  25. */
  26. class HttpResponse
  27. {
  28. private $content;
  29. private $body;
  30. private $header;
  31. private $requestId;
  32. private $errorMessage;
  33. private $contentType;
  34. private $httpStatusCode;
  35. public function getContent()
  36. {
  37. return $this->content;
  38. }
  39. public function setContent($content)
  40. {
  41. $this->content = $content;
  42. }
  43. public function setHeader($header)
  44. {
  45. $this->header = $header;
  46. }
  47. public function getHeader()
  48. {
  49. return $this->header;
  50. }
  51. public function setBody($body)
  52. {
  53. $this->body = $body;
  54. }
  55. public function getBody()
  56. {
  57. return $this->body;
  58. }
  59. public function getRequestId()
  60. {
  61. return $this->requestId;
  62. }
  63. public function getErrorMessage()
  64. {
  65. return $this->errorMessage;
  66. }
  67. public function getHttpStatusCode()
  68. {
  69. return $this->httpStatusCode;
  70. }
  71. public function setHttpStatusCode($httpStatusCode)
  72. {
  73. $this->httpStatusCode = $httpStatusCode;
  74. }
  75. public function getContentType()
  76. {
  77. return $this->contentType;
  78. }
  79. public function setContentType($contentType)
  80. {
  81. $this->contentType = $contentType;
  82. }
  83. public function getSuccess()
  84. {
  85. if(200 <= $this->httpStatusCode && 300 > $this->httpStatusCode)
  86. {
  87. return true;
  88. }
  89. return false;
  90. }
  91. /**
  92. *根据headersize大小,区分返回的header和body
  93. */
  94. public function setHeaderSize($headerSize) {
  95. if (0 < $headerSize && 0 < strlen($this->content)) {
  96. $this->header = substr($this->content, 0, $headerSize);
  97. self::extractKey();
  98. }
  99. if (0 < $headerSize && $headerSize < strlen($this->content)) {
  100. $this->body = substr($this->content, $headerSize);
  101. }
  102. }
  103. /**
  104. *提取header中的requestId和errorMessage
  105. */
  106. private function extractKey() {
  107. if (0 < strlen($this->header)) {
  108. $headers = explode("\r\n", $this->header);
  109. foreach ($headers as $value) {
  110. if(strpos($value, "X-Ca-Request-Id:") !== false)
  111. {
  112. $this->requestId = trim(substr($value, strlen("X-Ca-Request-Id:")));
  113. }
  114. if(strpos($value, "X-Ca-Error-Message:") !== false)
  115. {
  116. $this->errorMessage = trim(substr($value, strlen("X-Ca-Error-Message:")));
  117. }
  118. }
  119. }
  120. }
  121. }