企微短剧业务系统

HttpService.php 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Support\EmailQueue;
  5. class HttpService
  6. {
  7. /*
  8. * 封装HTTP请求 -- POST
  9. * */
  10. public static function httpPost($url, $data = null,$json = FALSE, $headers = null){
  11. //创建了一个curl会话资源,成功返回一个句柄;
  12. $curl = curl_init();
  13. //设置url
  14. curl_setopt($curl, CURLOPT_URL, $url);
  15. //设置为FALSE 禁止 cURL 验证对等证书(peer’s certificate)
  16. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  17. //设置为 1 是检查服务器SSL证书中是否存在一个公用名(common name)
  18. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  19. if (!empty($data)){
  20. //设置请求为POST
  21. curl_setopt($curl, CURLOPT_POST, 1);
  22. //curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60); //最长的可忍受的连接时间
  23. //设置POST的数据域
  24. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  25. if($json){
  26. // curl_setopt($curl, CURLOPT_HEADER, 0);
  27. // curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  28. // 'Content-Type: application/json; charset=utf-8',
  29. // 'Content-Length: ' . strlen($data)
  30. // )
  31. // );
  32. $header = array(
  33. 'Content-Type: application/json; charset=utf-8',
  34. 'Content-Length: ' . strlen($data)
  35. );
  36. if(!empty($headers)) {
  37. $header = array_merge($header, $headers);
  38. }
  39. curl_setopt($curl, CURLOPT_HEADER, 0);
  40. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  41. }
  42. }
  43. //设置是否将响应结果存入变量,1是存入,0是直接输出
  44. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  45. //然后将响应结果存入变量
  46. $output = curl_exec($curl);
  47. //关闭这个curl会话资源
  48. curl_close($curl);
  49. return $output;
  50. }
  51. /*
  52. * 封装HTTP请求 -- GET
  53. * */
  54. public static function httpGet($url, $headers = null){
  55. $curl = curl_init(); // 启动一个CURL会话
  56. curl_setopt($curl, CURLOPT_URL, $url);
  57. curl_setopt($curl, CURLOPT_HEADER, 0);
  58. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  59. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  60. //curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
  61. if ($headers) {
  62. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // 设置请求的header
  63. }
  64. $output = curl_exec($curl); //返回api的json对象
  65. //关闭URL请求
  66. curl_close($curl);
  67. return $output; //返回json对象
  68. }
  69. /**
  70. * 封装HTTP请求 -- 通过图片全路径上传图片
  71. * */
  72. public static function httpUpload($url, $filePath, $retry=0)
  73. {
  74. if(strstr($filePath, '../') !== false) {
  75. $filePathUpload = public_path($filePath);
  76. } else {
  77. $filePathUpload = $filePath;
  78. }
  79. $file_data = array("media" => new \CURLFile($filePathUpload));
  80. $curl = curl_init();
  81. curl_setopt($curl , CURLOPT_URL , $url);
  82. curl_setopt($curl , CURLOPT_RETURNTRANSFER, 1);
  83. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  84. curl_setopt($curl , CURLOPT_POST, 1);
  85. curl_setopt($curl , CURLOPT_POSTFIELDS, $file_data);
  86. $output = curl_exec($curl);
  87. $responseData = json_decode($output, true);
  88. if ($output === FALSE || !isset($responseData['errcode'])){
  89. Log::logError('上传图片HTTP请求失败', [
  90. 'error' => curl_error($curl),
  91. 'url' => $url,
  92. 'upload_path' => $filePathUpload,
  93. 'path' => $filePath,
  94. 'retry' => $retry
  95. ], 'HttpUploadError');
  96. if($retry<=5) { // 发起重试
  97. sleep(1);
  98. $retry++;
  99. return self::httpUpload($url, $filePath, $retry);
  100. } else { // 报警
  101. EmailQueue::rPush('图片全路径上传失败', curl_error($curl), ['xiaohua.hou@kuxuan-inc.com'], '图片全路径上传失败');
  102. }
  103. }
  104. curl_close($curl);
  105. return $output;
  106. }
  107. /*
  108. * 获取响应头-不返回响应体
  109. * */
  110. public static function getRedirectUrl($url) {
  111. $redirect_url = false;
  112. $ch = curl_init();
  113. curl_setopt($ch, CURLOPT_URL, $url);
  114. curl_setopt($ch, CURLOPT_HEADER, TRUE);
  115. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  116. curl_setopt($ch, CURLOPT_SSLVERSION, false);
  117. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  118. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  119. curl_setopt($ch, CURLOPT_NOBODY, TRUE);//不返回请求体内容
  120. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);//允许请求的链接跳转
  121. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  122. $content = curl_exec($ch);
  123. if($content === false){
  124. echo 'Curl error: ' . curl_error($ch);
  125. }
  126. if(!curl_errno($ch)) {
  127. $redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);//获取最终请求的url地址
  128. }
  129. return $redirect_url;
  130. }
  131. /**
  132. * @param $url
  133. * @param array $body
  134. * @param array $headers
  135. * @param bool $jsonType
  136. * @param int $timeout 设置等待时间。秒,注意,若超过了等待时间,依然没有执行完毕,curl_exec() 将会返回false
  137. * @param bool $autoRedirect
  138. * @param bool $showResponseHeader
  139. * @return mixed
  140. */
  141. public static function curl_post($url, $body = [], array $headers = [], $jsonType = true, $timeout = 30)
  142. {
  143. $opt = [
  144. CURLOPT_URL => $url,
  145. //设置获取的信息以文件流的形式返回,而不是直接输出。
  146. CURLOPT_RETURNTRANSFER => true, //要求结果为字符串且输出到屏幕上
  147. //https请求 不验证证书和hosts
  148. CURLOPT_SSL_VERIFYPEER => false,
  149. CURLOPT_SSL_VERIFYHOST => false,
  150. CURLOPT_POST => true,
  151. CURLOPT_HEADER => false, // 不输出响应头信息
  152. CURLOPT_HTTPHEADER => $headers,
  153. CURLOPT_POSTFIELDS => http_build_query($body),
  154. CURLOPT_TIMEOUT => $timeout,
  155. ];
  156. if ($jsonType) {
  157. $headers[] = 'Content-Type: application/json';
  158. $opt[CURLOPT_HTTPHEADER] = $headers;
  159. $opt[CURLOPT_POSTFIELDS] = json_encode($body);
  160. }
  161. $curl = curl_init(); // 初始化
  162. curl_setopt_array($curl, $opt);
  163. $output = curl_exec($curl); // 执行
  164. curl_close($curl); // 关闭cURL
  165. return $output;
  166. }
  167. /*
  168. * 封装HTTP请求 -- POST
  169. * */
  170. public static function apiPost($url, $data = null,$json = FALSE, $headers = null){
  171. //创建了一个curl会话资源,成功返回一个句柄;
  172. $curl = curl_init();
  173. //设置url
  174. curl_setopt($curl, CURLOPT_URL, $url);
  175. //设置为FALSE 禁止 cURL 验证对等证书(peer’s certificate)
  176. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  177. //设置为 1 是检查服务器SSL证书中是否存在一个公用名(common name)
  178. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  179. curl_setopt($curl, CURLOPT_HEADER, TRUE); //表示需要response header
  180. curl_setopt($curl, CURLOPT_NOBODY, FALSE); //表示需要response body
  181. if (!empty($data)){
  182. //设置请求为POST
  183. curl_setopt($curl, CURLOPT_POST, 1);
  184. //curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60); //最长的可忍受的连接时间
  185. //设置POST的数据域
  186. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  187. }
  188. if($json){
  189. $header = array(
  190. 'Content-Type: application/json; charset=utf-8',
  191. // 'Content-Length: ' . strlen($data)
  192. );
  193. if(!empty($headers)) {
  194. $header = array_merge($header, $headers);
  195. }
  196. curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  197. }
  198. //设置是否将响应结果存入变量,1是存入,0是直接输出
  199. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  200. //然后将响应结果存入变量
  201. $output = curl_exec($curl);
  202. $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
  203. //关闭这个curl会话资源
  204. curl_close($curl);
  205. $header = substr($output, 0, $headerSize);
  206. $body = substr($output, $headerSize);
  207. return [$header, $body];
  208. }
  209. }