PHP and Royal Mail Tracking API

We are having trouble setting up the PHP SOAP Client to use the Royal Mail Tracking API. We have an account configured with Royal Mail, and our ID and secret. We can make it work with SOAPUI, but when we try to implement it in PHP, we always get the "Invalid version" error. We have a WSDL file locally (which is provided by Royal Mail through the developer portal), this works with SOAPUI, but not with PHP SOAP Client. We hoped that someone could see that we were doing something wrong. I will send the code below, but I will not indicate our secret and identifier 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('Ymd\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 tried to use SOAP_1_1 and SOAP_1_2 for "soap_version", but it always returns with the error "Invalid version".

Hope someone can help.

+5
source share
1 answer

You need to set the header key inside the http array as follows:

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

Source: https://habr.com/ru/post/1242692/


All Articles