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