Files
zyt/server/debug_kuaidi100.php
T
2026-04-07 18:13:03 +08:00

165 lines
4.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 调试快递100 API响应
*/
// 从 .env 读取配置
$envFile = __DIR__ . '/.env';
$envContent = file_get_contents($envFile);
$lines = explode("\n", $envContent);
$config = [];
foreach ($lines as $line) {
$line = trim($line);
if (empty($line) || $line[0] === '#' || $line[0] === ';' || $line[0] === '[') {
continue;
}
if (strpos($line, '=') !== false) {
list($key, $value) = explode('=', $line, 2);
$key = trim($key);
$value = trim($value);
$value = trim($value, '"\'');
$config[$key] = $value;
}
}
$customer = $config['LOGISTICS_KUAIDI100_CUSTOMER'] ?? '';
$key = $config['LOGISTICS_KUAIDI100_KEY'] ?? '';
if (empty($customer) || empty($key)) {
die("错误:未配置快递100 CUSTOMER 或 KEY\n");
}
echo "===========================================\n";
echo "快递100 API 调试\n";
echo "===========================================\n\n";
// 测试用例
$testCases = [
[
'name' => '京东快递',
'com' => 'jingdong', // 修正为正确的编码
'num' => 'JD0230761381812',
'phone' => ''
],
[
'name' => '极兔速递',
'com' => 'jtexpress',
'num' => 'JT5472795424064',
'phone' => ''
],
];
foreach ($testCases as $test) {
echo "测试: {$test['name']}\n";
echo "-------------------------------------------\n";
echo "快递公司编码: {$test['com']}\n";
echo "运单号: {$test['num']}\n\n";
$paramArr = [
'com' => $test['com'],
'num' => $test['num'],
'resultv2' => '1',
];
if ($test['phone'] !== '') {
$paramArr['phone'] = $test['phone'];
}
$paramJson = json_encode($paramArr, JSON_UNESCAPED_UNICODE);
$sign = strtoupper(md5($paramJson . $key . $customer));
$postBody = http_build_query([
'customer' => $customer,
'param' => $paramJson,
'sign' => $sign,
]);
echo "请求参数:\n";
echo " customer: " . substr($customer, 0, 8) . "****\n";
echo " param: {$paramJson}\n";
echo " sign: " . substr($sign, 0, 16) . "****\n\n";
// 发送请求
$url = 'https://poll.kuaidi100.com/poll/query.do';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postBody);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/x-www-form-urlencoded',
]);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
echo "HTTP状态码: {$httpCode}\n";
if ($error) {
echo "CURL错误: {$error}\n\n";
continue;
}
if ($response === false || $response === '') {
echo "错误: 无响应\n\n";
continue;
}
echo "原始响应:\n";
echo $response . "\n\n";
$json = json_decode($response, true);
if (!is_array($json)) {
echo "错误: 无法解析JSON\n\n";
continue;
}
echo "解析后的响应:\n";
echo " result: " . var_export($json['result'] ?? null, true) . "\n";
echo " message: " . ($json['message'] ?? 'N/A') . "\n";
echo " returnCode: " . ($json['returnCode'] ?? 'N/A') . "\n";
if (isset($json['data']) && is_array($json['data'])) {
echo " data: " . count($json['data']) . " 条轨迹记录\n";
if (count($json['data']) > 0) {
echo "\n 最新轨迹:\n";
$latest = $json['data'][0];
echo " 时间: " . ($latest['ftime'] ?? $latest['time'] ?? 'N/A') . "\n";
echo " 内容: " . ($latest['context'] ?? 'N/A') . "\n";
}
} else {
echo " data: 无数据\n";
}
echo "\n";
}
echo "===========================================\n";
echo "调试完成\n";
echo "===========================================\n\n";
echo "常见错误码说明:\n";
echo "-------------------------------------------\n";
echo "200: 查询成功\n";
echo "500: 服务器错误\n";
echo "501: 重复提交\n";
echo "600: 您不是合法的订阅者\n";
echo "601: SIGN签名错误\n";
echo "700: 订阅方的订阅数据已达到上限\n";
echo "701: 快递公司参数异常\n";
echo "702: 快递单号参数异常\n";
echo "703: 查询无结果\n";
echo "704: 快递公司识别失败\n\n";
echo "如果返回 '找不到对应公司',可能原因:\n";
echo "1. 快递公司编码错误(如:jd 应该是 jingdong\n";
echo "2. 快递100不支持该快递公司\n";
echo "3. 需要使用快递100的自动识别功能\n\n";