正在显示
2 个修改的文件
包含
82 行增加
和
1 行删除
| @@ -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,60 @@ class RequestUrlLog extends Command | @@ -25,5 +27,60 @@ 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 | + $param = json_decode($v['param'],true); | ||
| 50 | + $result = $this->postRequest($url,$param); | ||
| 51 | + echo '执行的url:' . $url . PHP_EOL . '返回的结果:'.json_encode($result,true) . date('Y-m-d H:i:s'); | ||
| 52 | + //更新请求结果 | ||
| 53 | + $requestUrlModel->edit(['text'=>json_encode($result,true)],['id'=>$v['id']]); | ||
| 54 | + } | ||
| 55 | + } | ||
| 56 | + return true; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + /** | ||
| 60 | + * @remark :请求设置 | ||
| 61 | + * @name :postRequest | ||
| 62 | + * @author :lyh | ||
| 63 | + * @method :post | ||
| 64 | + * @time :2025/3/10 11:36 | ||
| 65 | + */ | ||
| 66 | + public function postRequest($url, $postData) { | ||
| 67 | + $ch = curl_init(); | ||
| 68 | + curl_setopt($ch, CURLOPT_URL, $url); | ||
| 69 | + curl_setopt($ch, CURLOPT_POST, true); | ||
| 70 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); | ||
| 71 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
| 72 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | ||
| 73 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); | ||
| 74 | + curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); | ||
| 75 | + // 记录请求开始时间 | ||
| 76 | + $startTime = microtime(true); | ||
| 77 | + $response = curl_exec($ch); | ||
| 78 | + // 记录请求结束时间 | ||
| 79 | + $endTime = microtime(true); | ||
| 80 | + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | ||
| 81 | + $requestTime = round(($endTime - $startTime) * 1000, 2); // 转换为毫秒 | ||
| 82 | + curl_close($ch); | ||
| 83 | + return ['response' => $response, 'http_code' => $httpCode, 'request_time_ms' => $requestTime]; | ||
| 84 | + } | ||
| 85 | + | ||
| 29 | } | 86 | } |
app/Models/Com/RequestUrl.php
0 → 100644
| 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 | +} |
-
请 注册 或 登录 后发表评论