作者 刘锟

Merge remote-tracking branch 'origin/master' into akun

@@ -9,6 +9,8 @@ @@ -9,6 +9,8 @@
9 9
10 namespace App\Console\Commands\RequestUrlLog; 10 namespace App\Console\Commands\RequestUrlLog;
11 11
  12 +use App\Models\Com\RequestUrl;
  13 +use App\Models\Project\Project;
12 use Illuminate\Console\Command; 14 use Illuminate\Console\Command;
13 15
14 class RequestUrlLog extends Command 16 class RequestUrlLog extends Command
@@ -25,5 +27,72 @@ class RequestUrlLog extends Command @@ -25,5 +27,72 @@ class RequestUrlLog extends Command
25 * 27 *
26 * @var string 28 * @var string
27 */ 29 */
28 - protected $description = '统计昨日数据'; 30 + protected $description = '请求日志统计';
  31 +
  32 + /**
  33 + * @remark :请求接口是否异常
  34 + * @name :handle
  35 + * @author :lyh
  36 + * @method :post
  37 + * @time :2025/3/10 10:51
  38 + */
  39 + public function handle(){
  40 + //获取需要请求的接口
  41 + $requestUrlModel = new RequestUrl();
  42 + $urlList = $requestUrlModel->list(['status'=>0]);
  43 + foreach ($urlList as $v){
  44 + //循环请求设置
  45 + if($v['method'] == 'get'){
  46 +
  47 + }else{
  48 + $url = $v['url'];
  49 + $result = $this->postRequest($url,$v['param']);
  50 + echo '执行的url:' . $url . PHP_EOL . '返回的结果:'.json_encode($result,true) . date('Y-m-d H:i:s').PHP_EOL;
  51 + //更新请求结果
  52 + $requestUrlModel->edit(['text'=>json_encode($result,true)],['id'=>$v['id']]);
  53 + }
  54 + }
  55 + return true;
  56 + }
  57 +
  58 + /**
  59 + * @remark :请求设置
  60 + * @name :postRequest
  61 + * @author :lyh
  62 + * @method :post
  63 + * @time :2025/3/10 11:36
  64 + */
  65 + public function postRequest($url, $postData)
  66 + {
  67 + if (empty($header)) {
  68 + $header = array(
  69 + "Accept: application/json",
  70 + "Content-Type:application/json;charset=utf-8",
  71 + );
  72 + }
  73 + $ch = curl_init();
  74 + curl_setopt($ch, CURLOPT_URL, $url);
  75 + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  76 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  77 + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  78 + curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  79 + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  80 + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  81 + curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  82 + curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  83 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  84 + // 记录请求开始时间
  85 + $startTime = microtime(true);
  86 + $res = curl_exec($ch);
  87 + // 记录请求结束时间
  88 + $endTime = microtime(true);
  89 + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  90 + if (curl_errno($ch)) {
  91 + curl_error($ch);
  92 + }
  93 + $requestTime = round(($endTime - $startTime) * 1000, 2); // 转换为毫秒
  94 + curl_close($ch);
  95 + return ['response' => $res, 'http_code' => $httpCode, 'request_time_ms' => $requestTime];
  96 + }
  97 +
29 } 98 }
  1 +<?php
  2 +/**
  3 + * @remark :
  4 + * @name :RequestUrl.php
  5 + * @author :lyh
  6 + * @method :post
  7 + * @time :2025/3/10 10:57
  8 + */
  9 +
  10 +namespace App\Models\Com;
  11 +
  12 +use App\Models\Base;
  13 +
  14 +/**
  15 + * @remark :模拟请求的url
  16 + * @name :RequestUrl
  17 + * @author :lyh
  18 + * @method :post
  19 + * @time :2025/3/10 10:57
  20 + */
  21 +class RequestUrl extends Base
  22 +{
  23 + protected $table = 'gl_request_url_log';
  24 +}