You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改PHP版UPS Tracking API的响应语言?

可以设置UPS Tracking API响应为其他语言!

当然没问题!UPS Tracking API支持通过在请求中指定语言参数来获取对应语言的响应。你只需要在TrackRequest的Request节点下添加Language子节点,指定对应的语言代码即可。

修改后的代码示例

下面是在你现有代码基础上调整后的版本,我添加了指定中文简体(zh-CN)的配置:

// 配置
$accessLicenseNumber = "xxx";
$userId = "xxx";
$password = "xxx";
$endpointurl = 'https://onlinetools.ups.com/ups.app/xml/Track';
try {
    // 创建AccessRequest XML
    $accessRequestXML = new SimpleXMLElement ("<AccessRequest></AccessRequest>");
    $accessRequestXML->addChild ("AccessLicenseNumber", $accessLicenseNumber);
    $accessRequestXML->addChild ("UserId", $userId);
    $accessRequestXML->addChild ("Password", $password);
    // 创建TrackRequest XML
    $trackRequestXML = new SimpleXMLElement ("<TrackRequest></TrackRequest >");
    $request = $trackRequestXML->addChild ('Request');
    $request->addChild ("RequestAction", "Track");
    $request->addChild ("RequestOption", "activity");
    // 添加语言参数,指定中文简体
    $request->addChild ("Language", "zh-CN");
    $trackRequestXML->addChild ("TrackingNumber", "xxx");
    $requestXML = $accessRequestXML->asXML () . $trackRequestXML->asXML ();
    $form = array (
        'http' => array (
            'method' => 'POST',
            'header' => 'Content-type: application/x-www-form-urlencoded',
            'content' => $requestXML
        )
    );
    // 创建请求上下文
    $request = stream_context_create ($form);
    $browser = fopen ($endpointurl, 'rb', false, $request);
    if (! $browser) {
        throw new Exception ("Connection failed.");
    }
    // 获取响应
    $response = stream_get_contents ($browser);
    fclose ($browser);
    if ($response == false) {
        throw new Exception ("Bad data.");
    } else {
        // 获取响应状态
        $resp = new SimpleXMLElement ($response);
    }
    Header ('Content-type: text/xml');
} catch ( Exception $ex ) {}

额外说明

  • 你可以替换zh-CN为UPS支持的其他语言代码,比如:
    • en-US:英文(默认)
    • fr-FR:法文
    • es-ES:西班牙文
    • ja-JP:日文
  • 注意要使用UPS官方认可的语言代码,这样才能确保返回正确的本地化响应内容。

内容的提问来源于stack exchange,提问作者Trombone0904

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:14:29