From 59b7c66a8910f46ab9fed7b71adc6469251d1442 Mon Sep 17 00:00:00 2001 From: muzihuaner Date: Wed, 18 Mar 2026 12:49:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/kd/index.php | 217 ++++++++++++++++++++++++++--------------------- 1 file changed, 119 insertions(+), 98 deletions(-) diff --git a/api/kd/index.php b/api/kd/index.php index 1d5f19b..5c6b49e 100644 --- a/api/kd/index.php +++ b/api/kd/index.php @@ -1,123 +1,144 @@ -info = $array; - $this->parametersexception(); + $this->process(); } - public function parametersexception() + + private function process() { - $info = $this->info; - if(!$info['id']) return $this->result(['code'=>-1, 'text'=>'请输入需要查询的快递单号']); - if(!preg_match('/^[a-z0-9]+$/im', $info['id'])) return $this->result(['code'=>-1, 'text'=>'请输入需要查询的快递单号']); - $this->getCookie(); - $this->start(); - return true; + if (!$this->info['id']) { + $this->output(['code' => -1, 'text' => '请输入需要查询的快递单号']); + return; + } + if (!preg_match('/^[a-z0-9]+$/im', $this->info['id'])) { + $this->output(['code' => -1, 'text' => '请输入需要查询的快递单号']); + return; + } + + try { + $this->getCookie(); + $this->queryExpress(); + } catch (Exception $e) { + $this->output(['code' => -3, 'text' => $e->getMessage()]); + } } - public function start() + + private function getCookie() { + $url = 'https://m.kuaidi100.com/result.jsp?nu=' . $this->info['id']; + $response = need::teacher_curl($url, ['GetCookie' => true]); + + if (!isset($response['Cookie']) || !isset($response['Cookie'][1])) { + throw new Exception('获取Cookie失败'); + } + + $this->Cookie = implode('; ', $response['Cookie'][1]); + if (!preg_match('/token=(.+?);/i', $this->Cookie, $matches)) { + throw new Exception('获取Token失败'); + } + $this->token = $matches[1]; + } + + private function getExpressType() + { + $autoUrl = 'https://m.kuaidi100.com/apicenter/kdquerytools.do?method=autoComNum&text=' . $this->info['id']; + $result = json_decode(need::teacher_curl($autoUrl, [ + 'post' => 'token=&platfrom=MWWW', + 'cookie' => $this->Cookie + ]), true); + + if (!isset($result['auto'][0])) { + throw new Exception('暂不支持该快递'); + } + return $result['auto'][0]['comCode']; + } + + private function queryExpress() + { + $type = $this->getExpressType(); $queryUrl = 'https://m.kuaidi100.com/query'; $rand = lcg_value(); - $type = $this->getType(); - if(!$type) - { - return $this->result(['code'=>-2, 'text'=>'暂不支持该快递']); - } else { - $post = 'postid='.$this->info['id'].'&id=1&valicode=&temp='.$rand.'&type='.$type.'&phone=&token='.$this->token.'&platform=MWWW'; - $query = json_decode(need::teacher_curl($queryUrl, [ - 'post'=>$post, - 'cookie'=>$this->Cookie, - 'Header'=>[ - 'Host: m.kuaidi100.com', - 'Connection: keep-alive', - 'Content-Length: '.strlen($post), - 'Accept: application/json, text/javascript, */*; q=0.01', - 'X-Requested-With: XMLHttpRequest', - 'User-Agent: Mozilla/5.0 (Linux; Android 7.1.2; OPPO R11t Build/N6F26Q; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/81.0.4044.117 Mobile Safari/537.36', - 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8', - 'Origin: https://m.kuaidi100.com', - 'Sec-Fetch-Site: same-origin', - 'Sec-Fetch-Mode: cors', - 'Sec-Fetch-Dest: empty', - 'Referer: https://m.kuaidi100.com/result.jsp?nu=JD0092947910127', - 'Accept-Encoding: gzip, deflate', - 'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7' - ], - 'ua'=>'Mozilla/5.0 (Linux; Android 7.1.2; OPPO R11t Build/N6F26Q; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/81.0.4044.117 Mobile Safari/537.36', - 'refer'=>'https://m.kuaidi100.com/result.jsp?nu='.$this->info['id'] - ]), true); - // print_r($query); - if($query['message'] == 'ok' && isset($query['data'][0]) && $query['data'][0]) - { - $message = []; - foreach($query['data'] as $v) - { - $message[] = '时间:'.$v['time'].'-'. $v['ftime']; - $message[] = '事件:'.$v['context']; - if (isset($v['local'])) $message[] = "地点:{$v['local']}\n"; - } - return $this->result([ - 'code'=>1, - 'text'=>'获取成功', - 'data'=>$query['data'] - ], trim(join("\n", $message))); - } else { - return $this->result(['code'=>-3, 'text'=>$query['message']]);//'未查询到有关于'.$this->info['id'].'的快递信息']); - } - } - } - public function getCookie() - { - $url = 'https://m.kuaidi100.com/result.jsp?nu='.$this->info['id']; - $Cookie = join('; ', need::teacher_curl($url, ['GetCookie'=>true])['Cookie'][1]); - preg_match('/token=(.+);/i', $Cookie, $token); - $this->Cookie = $Cookie; - $this->token = $token[1]; - return $Cookie; - } - public function getType() - { - $AutoUrl = 'https://m.kuaidi100.com/apicenter/kdquerytools.do?method=autoComNum&text='.$this->info['id']; - $Auto = json_decode(need::teacher_curl($AutoUrl, [ - 'post'=>'token=&platfrom=MWWW', - 'cookie'=>$this->Cookie + + $post = 'postid=' . $this->info['id'] . '&id=1&valicode=&temp=' . $rand . '&type=' . $type . + '&phone=&token=' . $this->token . '&platform=MWWW'; + + $response = json_decode(need::teacher_curl($queryUrl, [ + 'post' => $post, + 'cookie' => $this->Cookie, + 'Header' => [ + 'Host: m.kuaidi100.com', + 'Connection: keep-alive', + 'Content-Length: ' . strlen($post), + 'Accept: application/json, text/javascript, */*; q=0.01', + 'X-Requested-With: XMLHttpRequest', + 'User-Agent: Mozilla/5.0 (Linux; Android 7.1.2; OPPO R11t Build/N6F26Q; wv) AppleWebKit/537.36', + 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8', + 'Origin: https://m.kuaidi100.com', + 'Sec-Fetch-Site: same-origin', + 'Sec-Fetch-Mode: cors', + 'Sec-Fetch-Dest: empty', + 'Referer: https://m.kuaidi100.com/result.jsp?nu=' . $this->info['id'], + 'Accept-Encoding: gzip, deflate', + 'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7' + ], + 'ua' => 'Mozilla/5.0 (Linux; Android 7.1.2; OPPO R11t Build/N6F26Q; wv) AppleWebKit/537.36', + 'refer' => 'https://m.kuaidi100.com/result.jsp?nu=' . $this->info['id'] ]), true); - $type = false; - if(isset($Auto['auto'][0]) && $Auto['auto'][0]) - { - $type = $Auto['auto'][0]['comCode']; + + if ($response['message'] === 'ok' && isset($response['data'][0])) { + $textMessage = []; + foreach ($response['data'] as $item) { + $textMessage[] = '时间:' . $item['time'] . '-' . $item['ftime']; + $textMessage[] = '事件:' . $item['context']; + if (isset($item['local'])) { + $textMessage[] = '地点:' . $item['local']; + } + } + + $outputData = [ + 'code' => 1, + 'text' => '获取成功', + 'data' => $response['data'] + ]; + + if ($this->info['type'] === 'text') { + $this->output(['code' => 1, 'text' => implode("\n", $textMessage)], 'text'); + } else { + $this->output($outputData, 'json'); + } + } else { + $this->output(['code' => -3, 'text' => $response['message'] ?? '查询失败']); } - return $type; } - public function result($array, $message = null) + + private function output($data, $type = 'json') { - $message = $message ? $message : $array['text']; - $array = $array; - $info = $this->info; - $type = isset($info['type']) ? $info['type'] : 'json'; - Switch($type) - { - case 'text': - need::send($message, 'text'); - break; - default: - need::send($array, 'json'); - break; + if ($type === 'text') { + header('Content-type: text/plain; charset=utf-8'); + echo $data['text']; + } else { + header('Content-type: application/json; charset=utf-8'); + echo json_encode($data, JSON_UNESCAPED_UNICODE); } - // print_r($this->array); - return true; + exit; } } $request = need::request(); $id = isset($request['id']) ? $request['id'] : false; $type = isset($request['type']) ? $request['type'] : 'json'; -new kuaidi(['id'=>$id, 'type'=>$type]); \ No newline at end of file +new kuaidi(['id' => $id, 'type' => $type]); +?> \ No newline at end of file