HttpUtil.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils\Ocr\Util;
  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. use app\utils\Ocr\Constant\HttpSchema;
  27. use app\utils\Ocr\Constant\SystemHeader;
  28. use app\utils\Ocr\Http\HttpResponse;
  29. /**
  30. *http请求处理
  31. */
  32. class HttpUtil
  33. {
  34. public static function send($request, $readtimeout, $connectTimeout)
  35. {
  36. return self::DoHttp($request->getHost(),
  37. $request->getPath(),
  38. $request->getMethod(),
  39. $request->getAppKey(),
  40. $request->getAppSecret(),
  41. $readtimeout,
  42. $connectTimeout,
  43. $request->getHeaders(),
  44. $request->getQuerys(),
  45. $request->getBodys(),
  46. $request->getSignHeaders());
  47. }
  48. /**
  49. *请求Request
  50. */
  51. private static function DoHttp($host, $path, $method, $appKey, $appSecret, $readtimeout, $connectTimeout, $headers, $querys, $bodys, $signHeaderPrefixList)
  52. {
  53. $response = new HttpResponse();
  54. $headers = self::InitialBasicHeader($path, $appKey, $appSecret, $method, $headers, $querys, $bodys, $signHeaderPrefixList);
  55. $curl = self::InitHttpRequest($host, $path, $method, $readtimeout, $connectTimeout, $headers, $querys);
  56. $streams = array();
  57. if (is_array($bodys)) {
  58. if (0 < count($bodys)) {
  59. $body = "";
  60. foreach ($bodys as $itemKey => $itemValue) {
  61. if (0 < strlen($body)) {
  62. $body .= "&";
  63. }
  64. if (0 < strlen($itemValue) && 0 == strlen($itemKey))
  65. {
  66. $body .= $itemValue;
  67. array_push($streams, $itemValue);
  68. }
  69. if (0 < strlen($itemKey)) {
  70. $body .= "=";
  71. if (0 < strlen($itemValue)) {
  72. $body .= URLEncode($itemValue);
  73. }
  74. }
  75. }
  76. if (count($bodys) == count($streams) && 1 == count($streams)) {
  77. curl_setopt($curl, CURLOPT_POSTFIELDS, $streams[0]);
  78. } elseif (0 < count($bodys)) {
  79. curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($bodys));
  80. }
  81. }
  82. }
  83. $response->setContent(curl_exec($curl));
  84. $response->setHttpStatusCode(curl_getinfo($curl, CURLINFO_HTTP_CODE));
  85. $response->setContentType(curl_getinfo($curl, CURLINFO_CONTENT_TYPE));
  86. $response->setHeaderSize(curl_getinfo($curl, CURLINFO_HEADER_SIZE));
  87. curl_close($curl);
  88. return $response;
  89. }
  90. /**
  91. *准备请求Request
  92. */
  93. private static function InitHttpRequest($host, $path, $method, $readtimeout, $connectTimeout, $headers, $querys)
  94. {
  95. $url = $host;
  96. if (0 < strlen($path)) {
  97. $url.= $path;
  98. }
  99. $headerArray = array();
  100. if (is_array($headers)) {
  101. if (0 < count($headers)) {
  102. foreach ($headers as $itemKey => $itemValue) {
  103. if (0 < strlen($itemKey)) {
  104. array_push($headerArray, $itemKey.":".$itemValue);
  105. }
  106. }
  107. }
  108. }
  109. if (is_array($querys)) {
  110. if (0 < count($querys)) {
  111. $sb = "";
  112. foreach ($querys as $itemKey => $itemValue) {
  113. if (0 < strlen($sb)) {
  114. $sb .= "&";
  115. }
  116. if (0 < strlen($itemValue) && 0 == strlen($itemKey))
  117. {
  118. $sb .= $itemValue;
  119. }
  120. if (0 < strlen($itemKey)) {
  121. $sb .= $itemKey;
  122. if (0 < strlen($itemValue)) {
  123. $sb .= "=";
  124. $sb .= URLEncode($itemValue);
  125. }
  126. }
  127. }
  128. $url .= "?";
  129. $url .= $sb;
  130. }
  131. }
  132. $curl = curl_init();
  133. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  134. curl_setopt($curl, CURLOPT_URL, $url);
  135. curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
  136. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  137. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  138. curl_setopt($curl, CURLOPT_TIMEOUT, $readtimeout);
  139. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $connectTimeout);
  140. curl_setopt($curl, CURLOPT_HEADER, true);
  141. if (1 == strpos("$".$host, HttpSchema::HTTPS))
  142. {
  143. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  144. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  145. }
  146. curl_setopt($curl, CURLOPT_HEADER, true);
  147. return $curl;
  148. }
  149. /**
  150. *准备请求的基本header
  151. */
  152. private static function InitialBasicHeader($path, $appKey, $appSecret, $method, $headers, $querys, $bodys, $signHeaderPrefixList)
  153. {
  154. if (null == $headers) {
  155. $headers = array();
  156. }
  157. $sb = "";
  158. //时间戳
  159. date_default_timezone_set('PRC');
  160. $headers[SystemHeader::X_CA_TIMESTAMP] = strval(time()*1000);
  161. //防重放,协议层不能进行重试,否则会报NONCE被使用;如果需要协议层重试,请注释此行
  162. $headers[SystemHeader::X_CA_NONCE] = strval(self::NewGuid());
  163. $headers[SystemHeader::X_CA_KEY] = $appKey;
  164. $headers[SystemHeader::X_CA_SIGNATURE] = SignUtil::Sign($path, $method, $appSecret, $headers, $querys, $bodys, $signHeaderPrefixList);
  165. return $headers;
  166. }
  167. public static function CheckValidationResult($sender, $certificate, $chain, $errors)
  168. {
  169. return true;
  170. }
  171. private static function NewGuid()
  172. {
  173. mt_srand((double)microtime()*10000);
  174. $uuid = strtoupper(md5(uniqid(rand(), true)));
  175. return $uuid;
  176. }
  177. }