This commit is contained in:
muzihuaner
2026-03-18 12:49:43 +08:00
parent 46aa0bac39
commit 59b7c66a89

View File

@@ -1,123 +1,144 @@
<? <?php
header('Content-type: application/json'); header('Content-type: application/json; charset=utf-8');
error_reporting(E_ALL);
ini_set('display_errors', 0);
require __DIR__.'/../../need.php'; require __DIR__.'/../../need.php';
class kuaidi class kuaidi
{ {
private $info = []; private $info = [];
public $array = []; private $Cookie;
public $message; private $token;
public $Cookie, $token;
public function __construct(array $array) public function __construct(array $array)
{ {
$this->info = $array; $this->info = $array;
$this->parametersexception(); $this->process();
} }
public function parametersexception()
private function process()
{ {
$info = $this->info; if (!$this->info['id']) {
if(!$info['id']) return $this->result(['code'=>-1, 'text'=>'请输入需要查询的快递单号']); $this->output(['code' => -1, 'text' => '请输入需要查询的快递单号']);
if(!preg_match('/^[a-z0-9]+$/im', $info['id'])) return $this->result(['code'=>-1, 'text'=>'请输入需要查询的快递单号']); return;
$this->getCookie(); }
$this->start(); if (!preg_match('/^[a-z0-9]+$/im', $this->info['id'])) {
return true; $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'; $queryUrl = 'https://m.kuaidi100.com/query';
$rand = lcg_value(); $rand = lcg_value();
$type = $this->getType();
if(!$type) $post = 'postid=' . $this->info['id'] . '&id=1&valicode=&temp=' . $rand . '&type=' . $type .
{ '&phone=&token=' . $this->token . '&platform=MWWW';
return $this->result(['code'=>-2, 'text'=>'暂不支持该快递']);
} else { $response = json_decode(need::teacher_curl($queryUrl, [
$post = 'postid='.$this->info['id'].'&id=1&valicode=&temp='.$rand.'&type='.$type.'&phone=&token='.$this->token.'&platform=MWWW'; 'post' => $post,
$query = json_decode(need::teacher_curl($queryUrl, [ 'cookie' => $this->Cookie,
'post'=>$post, 'Header' => [
'cookie'=>$this->Cookie, 'Host: m.kuaidi100.com',
'Header'=>[ 'Connection: keep-alive',
'Host: m.kuaidi100.com', 'Content-Length: ' . strlen($post),
'Connection: keep-alive', 'Accept: application/json, text/javascript, */*; q=0.01',
'Content-Length: '.strlen($post), 'X-Requested-With: XMLHttpRequest',
'Accept: application/json, text/javascript, */*; q=0.01', 'User-Agent: Mozilla/5.0 (Linux; Android 7.1.2; OPPO R11t Build/N6F26Q; wv) AppleWebKit/537.36',
'X-Requested-With: XMLHttpRequest', 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
'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', 'Origin: https://m.kuaidi100.com',
'Content-Type: application/x-www-form-urlencoded; charset=UTF-8', 'Sec-Fetch-Site: same-origin',
'Origin: https://m.kuaidi100.com', 'Sec-Fetch-Mode: cors',
'Sec-Fetch-Site: same-origin', 'Sec-Fetch-Dest: empty',
'Sec-Fetch-Mode: cors', 'Referer: https://m.kuaidi100.com/result.jsp?nu=' . $this->info['id'],
'Sec-Fetch-Dest: empty', 'Accept-Encoding: gzip, deflate',
'Referer: https://m.kuaidi100.com/result.jsp?nu=JD0092947910127', 'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7'
'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']
'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
]), true); ]), true);
$type = false;
if(isset($Auto['auto'][0]) && $Auto['auto'][0]) if ($response['message'] === 'ok' && isset($response['data'][0])) {
{ $textMessage = [];
$type = $Auto['auto'][0]['comCode']; 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']; if ($type === 'text') {
$array = $array; header('Content-type: text/plain; charset=utf-8');
$info = $this->info; echo $data['text'];
$type = isset($info['type']) ? $info['type'] : 'json'; } else {
Switch($type) header('Content-type: application/json; charset=utf-8');
{ echo json_encode($data, JSON_UNESCAPED_UNICODE);
case 'text':
need::send($message, 'text');
break;
default:
need::send($array, 'json');
break;
} }
// print_r($this->array); exit;
return true;
} }
} }
$request = need::request(); $request = need::request();
$id = isset($request['id']) ? $request['id'] : false; $id = isset($request['id']) ? $request['id'] : false;
$type = isset($request['type']) ? $request['type'] : 'json'; $type = isset($request['type']) ? $request['type'] : 'json';
new kuaidi(['id'=>$id, 'type'=>$type]); new kuaidi(['id' => $id, 'type' => $type]);
?>