Result.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Tools;
  3. use App\Constants\ErrorCode;
  4. class Result
  5. {
  6. public static function success($data = [])
  7. {
  8. return static::result(ErrorCode::SUCCESS, ErrorCode::getMessage(ErrorCode::SUCCESS), $data);
  9. }
  10. public static function error($message = '', $code = ErrorCode::ERROR, $data = [])
  11. {
  12. if (empty($message)) {
  13. return static::result($code, ErrorCode::getMessage($code), $data);
  14. } else {
  15. return static::result($code, $message, $data);
  16. }
  17. }
  18. protected static function result($code, $message, $data)
  19. {
  20. return [
  21. 'code' => $code,
  22. 'message' => $message,
  23. 'data' => $data,
  24. ];
  25. }
  26. //判断连接是否正确
  27. public static function pageExists($url) {
  28. $ch = curl_init();
  29. // 设置 cURL 选项
  30. curl_setopt($ch, CURLOPT_URL, $url);
  31. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 返回响应作为字符串,而不是直接输出
  32. curl_setopt($ch, CURLOPT_NOBODY, false); // 如果你想检查内容,设置为 false
  33. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 允许重定向
  34. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100); // 设置连接超时时间
  35. curl_setopt($ch, CURLOPT_TIMEOUT, 200); // 设置读取数据的超时时间
  36. curl_setopt($ch, CURLOPT_FAILONERROR, true); // 在发生错误时返回 FALSE
  37. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'); // 设置用户代理,模拟浏览器请求
  38. // 执行 cURL 请求
  39. $response = curl_exec($ch);
  40. // 检查是否有 cURL 错误发生
  41. if (curl_errno($ch)) {
  42. curl_close($ch);
  43. return false;
  44. }
  45. // 获取 HTTP 状态码
  46. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  47. // 关闭 cURL
  48. curl_close($ch);
  49. // 判断 HTTP 状态码是否在 200-299 范围内(表示成功)
  50. // 或者你可以根据需要调整这个范围,比如包括 301, 302 重定向等
  51. return $httpCode;
  52. }
  53. public static function http_get($url)
  54. {
  55. $oCurl = curl_init();
  56. if (stripos($url, "https://") !== false) {
  57. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
  58. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  59. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  60. }
  61. curl_setopt($oCurl, CURLOPT_URL, $url);
  62. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  63. $sContent = curl_exec($oCurl);
  64. $aStatus = curl_getinfo($oCurl);
  65. curl_close($oCurl);
  66. if (intval($aStatus["http_code"]) == 200) {
  67. return $sContent;
  68. } else {
  69. return false;
  70. }
  71. }
  72. /**
  73. * POST 请求
  74. * @param string $url
  75. * @param array $param
  76. * @param boolean $post_file 是否文件上传
  77. * @return string content
  78. */
  79. public static function http_post($url, $data, $options = [])
  80. {
  81. // 初始化CURL会话
  82. $ch = curl_init($url);
  83. // 设置CURL选项
  84. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
  85. curl_setopt($ch, CURLOPT_HEADER, false); // 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
  86. curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的POST请求。
  87. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); // 要传递的POST数据,这里使用http_build_query将数组转换为URL编码的查询字符串。
  88. // 如果有额外的CURL选项,则合并它们
  89. if (!empty($options)) {
  90. curl_setopt_array($ch, $options);
  91. }
  92. // 执行CURL会话并获取响应
  93. $response = curl_exec($ch);
  94. // 检查是否有CURL错误
  95. if (curl_errno($ch)) {
  96. $error_msg = curl_error($ch);
  97. curl_close($ch);
  98. throw new Exception("CURL Error: $error_msg");
  99. }
  100. // 获取HTTP状态码
  101. $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  102. // 关闭CURL会话
  103. curl_close($ch);
  104. // 返回一个包含响应和HTTP状态码的数组
  105. $responseBody = $response;
  106. $headerEnd = strpos($response, "\r\n\r\n");
  107. if ($headerEnd !== false) {
  108. // 去除响应头,只保留响应体
  109. $responseBody = substr($response, $headerEnd + 4); // +4 是因为 "\r\n\r\n" 有4个字符
  110. // echo $responseBody; // 输出:This is the response body.
  111. } else {
  112. // 如果没有找到空行,可能响应格式不正确或没有响应头
  113. // echo "No headers found in response.";
  114. }
  115. return [
  116. 'response' => $responseBody,
  117. 'http_code' => $http_code
  118. ];
  119. }
  120. }