【问题标题】:PHP and Royal Mail Tracking APIPHP 和 Royal Mail 跟踪 API
【发布时间】:2016-02-10 09:59:20
【问题描述】:

我们在设置 PHP SOAP 客户端以使用 Royal Mail Tracking API 时遇到了一些问题。我们在 Royal Mail 上设置了一个帐户,并拥有我们的 ID 和机密信息。我们可以使用 SOAPUI 让它工作,但是当我们尝试在 PHP 中实现它时,我们总是得到一个“错误的版本”错误。我们在本地有 WSDL 文件(Royal Mail 通过他们的开发人员门户提供),它适用于 SOAPUI,但不适用于 PHP SOAP 客户端。我们希望有人能够查看我们是否做错了什么。我将在下面发布代码,但会在代码中省略我们的秘密和 ID。

<?php
ini_set('soap.wsdl_cache_enabled', 0);
ini_set('soap.wsdl_cache_ttl', 900);
ini_set('default_socket_timeout', 15);

$trackingNumber = 'F111111111JD';
$time = gmdate('Y-m-d\TH:i:s');

$intHeaders = [
    'dateTime' => $time,
    'version' => '1.0',
    'identification' => [
        'applicationId' => '***********',
        'transactionId' => 123456
    ]
];

$wsdl = 'WSDL/Tracking_API_V1_1_1.wsdl';

$options = array(
    'uri'=>'http://schemas.xmlsoap.org/soap/envelope/',
    'style'=>SOAP_RPC,
    'use'=>SOAP_ENCODED,
    'soap_version'=>SOAP_1_2,
    'cache_wsdl'=>WSDL_CACHE_NONE,
    'connection_timeout'=>15,
    'trace'=>true,
    'encoding'=>'UTF-8',
    'exceptions'=>true,
    'stream_context' => stream_context_create([
        "http" => [
            'Accept' => 'application/soap+xml',
            'X-IBM-Client-Secret' => '****',
            'X-IBM-Client-Id'=> '****'
        ]
    ])
);
try {
    $soap = new SoapClient($wsdl, $options);
    $data = $soap->getSingleItemHistory(['integrationHeader' => $intHeaders, 'trackingNumber' => $trackingNumber]);
}
catch(Exception $e) {
    die($e->getMessage());
}

var_dump($data);
die;

我们已尝试将 SOAP_1_1 和 SOAP_1_2 用于“soap_version”,但总是返回“错误版本”错误。

希望有人能够提供帮助。

【问题讨论】:

    标签: php soap


    【解决方案1】:

    您需要像这样在http 数组中设置header 键:

    'stream_context' => stream_context_create(
        [
            'http' =>
                [
                    'header'           => implode(
                        "\r\n",
                        [
                            'Accept: application/soap+xml',
                            'X-IBM-Client-Id: ' . $clientId,
                            'X-IBM-Client-Secret: ' . $clientSecret,
                        ]
                    ),
                ],
        ]
    )
    

    【讨论】:

    • 非常感谢。这对我们有用。抱歉,由于我不在并且无法访问该网站,因此迟到了回复您的答案。
    • @alexbibie,你拯救了我的一天。在我正在实施的一个银行 api 中,我用了 2 天的时间来处理类似的情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多