No Description

HttpService.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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)
  73. {
  74. if(strstr($filePath, '../') !== false) {
  75. $filePath = public_path($filePath);
  76. }
  77. $file_data = array("media" => new \CURLFile($filePath));
  78. $curl = curl_init();
  79. curl_setopt($curl , CURLOPT_URL , $url);
  80. curl_setopt($curl , CURLOPT_RETURNTRANSFER, 1);
  81. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
  82. curl_setopt($curl , CURLOPT_POST, 1);
  83. curl_setopt($curl , CURLOPT_POSTFIELDS, $file_data);
  84. $output = curl_exec($curl);
  85. if ($output === FALSE){
  86. # 增加报警
  87. EmailQueue::rPush('图片全路径上传失败', curl_error($curl), ['xiaohua.hou@kuxuan-inc.com'], '图片全路径上传失败');
  88. Log::logError('上传图片HTTP请求失败', [
  89. 'error' => curl_error($curl),
  90. 'url' => $url,
  91. 'path' => $filePath,
  92. ], 'HttpUploadError');
  93. }
  94. curl_close($curl);
  95. return $output;
  96. }
  97. /*
  98. * 获取响应头-不返回响应体
  99. * */
  100. public static function getRedirectUrl($url) {
  101. $redirect_url = false;
  102. $ch = curl_init();
  103. curl_setopt($ch, CURLOPT_URL, $url);
  104. curl_setopt($ch, CURLOPT_HEADER, TRUE);
  105. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  106. curl_setopt($ch, CURLOPT_SSLVERSION, false);
  107. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  108. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  109. curl_setopt($ch, CURLOPT_NOBODY, TRUE);//不返回请求体内容
  110. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);//允许请求的链接跳转
  111. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  112. $content = curl_exec($ch);
  113. if($content === false){
  114. echo 'Curl error: ' . curl_error($ch);
  115. }
  116. if(!curl_errno($ch)) {
  117. $redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);//获取最终请求的url地址
  118. }
  119. return $redirect_url;
  120. }
  121. /**
  122. * @param $url
  123. * @param array $body
  124. * @param array $headers
  125. * @param bool $jsonType
  126. * @param int $timeout 设置等待时间。秒,注意,若超过了等待时间,依然没有执行完毕,curl_exec() 将会返回false
  127. * @param bool $autoRedirect
  128. * @param bool $showResponseHeader
  129. * @return mixed
  130. */
  131. public static function curl_post($url, $body = [], array $headers = [], $jsonType = true, $timeout = 30)
  132. {
  133. $opt = [
  134. CURLOPT_URL => $url,
  135. //设置获取的信息以文件流的形式返回,而不是直接输出。
  136. CURLOPT_RETURNTRANSFER => true, //要求结果为字符串且输出到屏幕上
  137. //https请求 不验证证书和hosts
  138. CURLOPT_SSL_VERIFYPEER => false,
  139. CURLOPT_SSL_VERIFYHOST => false,
  140. CURLOPT_POST => true,
  141. CURLOPT_HEADER => false, // 不输出响应头信息
  142. CURLOPT_HTTPHEADER => $headers,
  143. CURLOPT_POSTFIELDS => http_build_query($body),
  144. CURLOPT_TIMEOUT => $timeout,
  145. ];
  146. if ($jsonType) {
  147. $headers[] = 'Content-Type: application/json';
  148. $opt[CURLOPT_HTTPHEADER] = $headers;
  149. $opt[CURLOPT_POSTFIELDS] = json_encode($body);
  150. }
  151. $curl = curl_init(); // 初始化
  152. curl_setopt_array($curl, $opt);
  153. $output = curl_exec($curl); // 执行
  154. curl_close($curl); // 关闭cURL
  155. return $output;
  156. }
  157. }