RequestUrlLog.php
6.1 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* @remark :
* @name :RequestUrlLog.php
* @author :lyh
* @method :post
* @time :2025/2/21 9:55
*/
namespace App\Console\Commands\Monitor;
use App\Helper\FormGlobalsoApi;
use App\Models\Com\RequestUrl;
use App\Models\Domain\DomainInfo;
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['url'] == 'getMonthInquiry'){
//随机获取一个项目,需要验证的其他方法
$projectModel = new Project();
$projectInfo = $projectModel->formatQuery(['type'=>2,'delete_status'=>0])->inRandomOrder()->first();
//获取对应项目的域名
$domainModel = new DomainInfo();
$domainInfo = $domainModel->read(['project_id'=>$projectInfo['id']]);
$result = $this->getMonthInquiry($domainInfo['domain'],date('Y-m'), 0);
$requestUrlModel->edit(['text'=>json_encode($result,true),'time'=>$result['$requestTime'],'http_code'=>$result['http_code']],['id'=>$v['id']]);
continue;
}
//循环请求设置
if($v['method'] == 'post'){
$result = $this->postRequest($v['url'],$v['param']);
echo '执行的url:' . $v['url'] . PHP_EOL . '返回的结果:'.json_encode($result,true) . date('Y-m-d H:i:s').PHP_EOL;
//更新请求结果
$requestUrlModel->edit(['text'=>json_encode($result,true),'time'=>$result['requestTime'],'http_code'=>$result['http_code']],['id'=>$v['id']]);
continue;
}else if($v['method'] == 'get'){
$result = $this->getRequest($v['url']);
echo '执行的url:' . $v['url'] . PHP_EOL . '返回的结果:'.json_encode($result,true) . date('Y-m-d H:i:s').PHP_EOL;
//更新请求结果
$requestUrlModel->edit(['text'=>json_encode($result,true),'time'=>$result['requestTime'],'http_code'=>$result['http_code']],['id'=>$v['id']]);
continue;
}else{
//todo::其他方式的验证
}
}
return true;
}
/**
* @remark :按月统计xunpan
* @name :getMonthInquiry
* @author :lyh
* @method :post
* @time :2025/3/10 14:15
*/
public function getMonthInquiry($url,$month,$is_upgrade = 0){
$url = 'https://'.$url.'/';
$token = md5($url.date("Y-m-d"));
$data = [
'domain' => $url,
'token' => $token,
'source'=> $is_upgrade ? '1,2,3,4,5' : '1,3,5',
'model' => 'month',
'sta_date'=>$month,
];
$queryString = http_build_query($data);
$url = 'https://www.globalso.site/api/external-interface/country_con/15243d63ed5a5738?'.$queryString;
$res = $this->getRequest($url);
return $res;
}
/**
* @remark :http_get请求
* @name :getRequest
* @author :lyh
* @method :post
* @time :2025/3/10 14:35
*/
public function getRequest($url){
$header[] = "content-type: application/json";
$ch1 = curl_init();
$timeout = 0;
curl_setopt($ch1, CURLOPT_URL, $url);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_ENCODING, '');
curl_setopt($ch1, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch1, CURLOPT_TIMEOUT, 120);
curl_setopt($ch1, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch1, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$access_txt = curl_exec($ch1);
$total_time = curl_getinfo($ch1, CURLINFO_TOTAL_TIME); // 获取请求总时间
$httpCode = curl_getinfo($ch1, CURLINFO_HTTP_CODE);
if (curl_errno($ch1)) {
curl_error($ch1);
}
curl_close($ch1);
$response = json_decode($access_txt, true);
return ['response' => $response, 'http_code' => $httpCode, 'request_time_ms' => $total_time];
}
/**
* @remark :请求设置
* @name :postRequest
* @author :lyh
* @method :post
* @time :2025/3/10 11:36
*/
public function postRequest($url, $postData)
{
$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);
$res = curl_exec($ch);
$total_time = curl_getinfo($ch, CURLINFO_TOTAL_TIME); // 获取请求总时间
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
curl_error($ch);
}
curl_close($ch);
$response = json_decode($res, true);
return ['response' => $response, 'http_code' => $httpCode, 'request_time_ms' => $total_time];
}
}