作者 lyh

gx数据

@@ -9,7 +9,9 @@ @@ -9,7 +9,9 @@
9 9
10 namespace App\Console\Commands\RequestUrlLog; 10 namespace App\Console\Commands\RequestUrlLog;
11 11
  12 +use App\Helper\FormGlobalsoApi;
12 use App\Models\Com\RequestUrl; 13 use App\Models\Com\RequestUrl;
  14 +use App\Models\Domain\DomainInfo;
13 use App\Models\Project\Project; 15 use App\Models\Project\Project;
14 use Illuminate\Console\Command; 16 use Illuminate\Console\Command;
15 17
@@ -40,22 +42,87 @@ class RequestUrlLog extends Command @@ -40,22 +42,87 @@ class RequestUrlLog extends Command
40 //获取需要请求的接口 42 //获取需要请求的接口
41 $requestUrlModel = new RequestUrl(); 43 $requestUrlModel = new RequestUrl();
42 $urlList = $requestUrlModel->list(['status'=>0]); 44 $urlList = $requestUrlModel->list(['status'=>0]);
  45 + //随机获取一个项目,需要验证的其他方法
  46 + $projectModel = new Project();
  47 + $projectInfo = $projectModel->formatQuery(['type'=>2,'delete_status'=>0])->inRandomOrder()->first();
  48 + //获取对应项目的域名
  49 + $domainModel = new DomainInfo();
  50 + $domainInfo = $domainModel->read(['project_id'=>$projectInfo['id']]);
43 foreach ($urlList as $v){ 51 foreach ($urlList as $v){
  52 + //需要单独验证的方法
  53 + if($v['url'] == 'getMonthInquiry'){
  54 + $result = $this->getMonthInquiry($domainInfo['domain'],date('Y-m'), 0);
  55 + $requestUrlModel->edit(['text'=>json_encode($result,true),'time'=>$result['$requestTime'],'http_code'=>$result['http_code']],['id'=>$v['id']]);
  56 + continue;
  57 + }
44 //循环请求设置 58 //循环请求设置
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; 59 + if($v['method'] == 'post'){
  60 + $result = $this->postRequest($v['url'],$v['param']);
  61 + echo '执行的url:' . $v['url'] . PHP_EOL . '返回的结果:'.json_encode($result,true) . date('Y-m-d H:i:s').PHP_EOL;
51 //更新请求结果 62 //更新请求结果
52 - $requestUrlModel->edit(['text'=>json_encode($result,true)],['id'=>$v['id']]); 63 + $requestUrlModel->edit(['text'=>json_encode($result,true),'time'=>$result['$requestTime'],'http_code'=>$result['http_code']],['id'=>$v['id']]);
  64 + continue;
53 } 65 }
54 } 66 }
55 return true; 67 return true;
56 } 68 }
57 69
58 /** 70 /**
  71 + * @remark :按月统计xunpan
  72 + * @name :getMonthInquiry
  73 + * @author :lyh
  74 + * @method :post
  75 + * @time :2025/3/10 14:15
  76 + */
  77 + public function getMonthInquiry($url,$month,$is_upgrade = 0){
  78 + $url = 'https://'.$url.'/';
  79 + $token = md5($url.date("Y-m-d"));
  80 + $data = [
  81 + 'domain' => $url,
  82 + 'token' => $token,
  83 + 'source'=> $is_upgrade ? '1,2,3,4,5' : '1,3,5',
  84 + 'model' => 'month',
  85 + 'sta_date'=>$month,
  86 + ];
  87 + $queryString = http_build_query($data);
  88 + $url = 'https://www.globalso.site/api/external-interface/country_con/15243d63ed5a5738?'.$queryString;
  89 + $res = $this->getRequest($url);
  90 + return $res;
  91 + }
  92 +
  93 + /**
  94 + * @remark :http_get请求
  95 + * @name :getRequest
  96 + * @author :lyh
  97 + * @method :post
  98 + * @time :2025/3/10 14:35
  99 + */
  100 + public function getRequest($url){
  101 + $header[] = "content-type: application/json";
  102 + $ch1 = curl_init();
  103 + $timeout = 0;
  104 + curl_setopt($ch1, CURLOPT_URL, $url);
  105 + curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
  106 + curl_setopt($ch1, CURLOPT_ENCODING, '');
  107 + curl_setopt($ch1, CURLOPT_MAXREDIRS, 10);
  108 + curl_setopt($ch1, CURLOPT_TIMEOUT, 120);
  109 + curl_setopt($ch1, CURLOPT_HTTPHEADER, $header);
  110 + curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);
  111 + curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true);
  112 + curl_setopt($ch1, CURLOPT_CUSTOMREQUEST, 'GET');
  113 + curl_setopt($ch1, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
  114 + $access_txt = curl_exec($ch1);
  115 + $total_time = curl_getinfo($ch1, CURLINFO_TOTAL_TIME); // 获取请求总时间
  116 + $httpCode = curl_getinfo($ch1, CURLINFO_HTTP_CODE);
  117 + if (curl_errno($ch1)) {
  118 + curl_error($ch1);
  119 + }
  120 + curl_close($ch1);
  121 + $response = json_decode($access_txt, true);
  122 + return ['response' => $response, 'http_code' => $httpCode, 'request_time_ms' => $total_time];
  123 + }
  124 +
  125 + /**
59 * @remark :请求设置 126 * @remark :请求设置
60 * @name :postRequest 127 * @name :postRequest
61 * @author :lyh 128 * @author :lyh
@@ -64,12 +131,10 @@ class RequestUrlLog extends Command @@ -64,12 +131,10 @@ class RequestUrlLog extends Command
64 */ 131 */
65 public function postRequest($url, $postData) 132 public function postRequest($url, $postData)
66 { 133 {
67 - if (empty($header)) {  
68 - $header = array(  
69 - "Accept: application/json",  
70 - "Content-Type:application/json;charset=utf-8",  
71 - );  
72 - } 134 + $header = array(
  135 + "Accept: application/json",
  136 + "Content-Type:application/json;charset=utf-8",
  137 + );
73 $ch = curl_init(); 138 $ch = curl_init();
74 curl_setopt($ch, CURLOPT_URL, $url); 139 curl_setopt($ch, CURLOPT_URL, $url);
75 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 140 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
@@ -81,18 +146,15 @@ class RequestUrlLog extends Command @@ -81,18 +146,15 @@ class RequestUrlLog extends Command
81 curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 146 curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
82 curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 147 curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
83 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 148 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
84 - // 记录请求开始时间  
85 - $startTime = microtime(true);  
86 $res = curl_exec($ch); 149 $res = curl_exec($ch);
87 - // 记录请求结束时间  
88 - $endTime = microtime(true); 150 + $total_time = curl_getinfo($ch, CURLINFO_TOTAL_TIME); // 获取请求总时间
89 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 151 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
90 if (curl_errno($ch)) { 152 if (curl_errno($ch)) {
91 curl_error($ch); 153 curl_error($ch);
92 } 154 }
93 - $requestTime = round(($endTime - $startTime) * 1000, 2); // 转换为毫秒  
94 curl_close($ch); 155 curl_close($ch);
95 - return ['response' => $res, 'http_code' => $httpCode, 'request_time_ms' => $requestTime]; 156 + $response = json_decode($res, true);
  157 + return ['response' => $response, 'http_code' => $httpCode, 'request_time_ms' => $total_time];
96 } 158 }
97 159
98 } 160 }