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;
}
if (!preg_match('/^[a-z0-9]+$/im', $this->info['id'])) {
$this->output(['code' => -1, 'text' => '请输入需要查询的快递单号']);
return;
}
try {
$this->getCookie(); $this->getCookie();
$this->start(); $this->queryExpress();
return true; } 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,
'Header'=>[
'Host: m.kuaidi100.com', 'Host: m.kuaidi100.com',
'Connection: keep-alive', 'Connection: keep-alive',
'Content-Length: '.strlen($post), 'Content-Length: ' . strlen($post),
'Accept: application/json, text/javascript, */*; q=0.01', 'Accept: application/json, text/javascript, */*; q=0.01',
'X-Requested-With: XMLHttpRequest', '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', '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', 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
'Origin: https://m.kuaidi100.com', 'Origin: https://m.kuaidi100.com',
'Sec-Fetch-Site: same-origin', 'Sec-Fetch-Site: same-origin',
'Sec-Fetch-Mode: cors', 'Sec-Fetch-Mode: cors',
'Sec-Fetch-Dest: empty', 'Sec-Fetch-Dest: empty',
'Referer: https://m.kuaidi100.com/result.jsp?nu=JD0092947910127', 'Referer: https://m.kuaidi100.com/result.jsp?nu=' . $this->info['id'],
'Accept-Encoding: gzip, deflate', 'Accept-Encoding: gzip, deflate',
'Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7' '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', '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'] 'refer' => 'https://m.kuaidi100.com/result.jsp?nu=' . $this->info['id']
]), true); ]), true);
// print_r($query);
if($query['message'] == 'ok' && isset($query['data'][0]) && $query['data'][0]) if ($response['message'] === 'ok' && isset($response['data'][0])) {
{ $textMessage = [];
$message = []; foreach ($response['data'] as $item) {
foreach($query['data'] as $v) $textMessage[] = '时间:' . $item['time'] . '-' . $item['ftime'];
{ $textMessage[] = '事件:' . $item['context'];
$message[] = '时间:'.$v['time'].'-'. $v['ftime']; if (isset($item['local'])) {
$message[] = '事件'.$v['context']; $textMessage[] = '地点' . $item['local'];
if (isset($v['local'])) $message[] = "地点:{$v['local']}\n";
} }
return $this->result([ }
'code'=>1,
'text'=>'获取成功', $outputData = [
'data'=>$query['data'] 'code' => 1,
], trim(join("\n", $message))); 'text' => '获取成功',
'data' => $response['data']
];
if ($this->info['type'] === 'text') {
$this->output(['code' => 1, 'text' => implode("\n", $textMessage)], 'text');
} else { } else {
return $this->result(['code'=>-3, 'text'=>$query['message']]);//'未查询到有关于'.$this->info['id'].'的快递信息']); $this->output($outputData, 'json');
}
} else {
$this->output(['code' => -3, 'text' => $response['message'] ?? '查询失败']);
} }
} }
}
public function getCookie() private function output($data, $type = 'json')
{ {
$url = 'https://m.kuaidi100.com/result.jsp?nu='.$this->info['id']; if ($type === 'text') {
$Cookie = join('; ', need::teacher_curl($url, ['GetCookie'=>true])['Cookie'][1]); header('Content-type: text/plain; charset=utf-8');
preg_match('/token=(.+);/i', $Cookie, $token); echo $data['text'];
$this->Cookie = $Cookie; } else {
$this->token = $token[1]; header('Content-type: application/json; charset=utf-8');
return $Cookie; echo json_encode($data, JSON_UNESCAPED_UNICODE);
} }
public function getType() exit;
{
$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);
$type = false;
if(isset($Auto['auto'][0]) && $Auto['auto'][0])
{
$type = $Auto['auto'][0]['comCode'];
}
return $type;
}
public function result($array, $message = null)
{
$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;
}
// print_r($this->array);
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]);
?>