Flyingearl
Flyingearl

Reputation: 679

PHP and Royal Mail Tracking API

We have been having some issues with setting up a PHP SOAP Client to make use of the Royal Mail Tracking API. We have an account all set up with Royal Mail and have our ID's and secrets. We can get it to work using SOAPUI but we are always getting a "Wrong Version" error when trying to implement it in PHP. We have the WSDL file locally (which Royal Mail provided via their developer portal) this works with SOAPUI but not PHP SOAP Client. We were hoping someone may be able to see if we are doing anything wrong. I'll post the code below but will omit our secret and ID from the code.

<?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;

We've tried using SOAP_1_1 and SOAP_1_2 for the 'soap_version' but it always comes back with the "Wrong Version" error.

Hope someone is able to help.

Upvotes: 5

Views: 1407

Answers (1)

alexbilbie
alexbilbie

Reputation: 1167

You need to set the header key inside the http array like so:

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

Upvotes: 7

Related Questions