How do you make a SoapCall using the PHP Soap class client - php

I am attempting to make a SOAP call in PHP using the PHP Soap Client class. I managed to connect to the WDSL file however it isn't accepting my parameters. Here are all the necessary information needed for this call. When I enter the following:
$wsdlUrl = 'https://irm.cooperboating.com/rdpwincentralsvc/irmpublic.asmx?WSDL';
$client = new SoapClient($wsdlUrl);
var_dump($client->__getFunctions());
var_dump($client->__getTypes());
I get:
// Output from getFunctions()
[26]=>
string(83) "GetCourseInformationResponse GetCourseInformation(GetCourseInformation $parameters)"
// Output from getTypes()
[117]=>
string(63) "struct GetCourseInformation {
GetCourseInformation_irmRQ RQ;
}"
[118]=>
string(152) "struct GetCourseInformation_irmRQ {
irmWebSvcCredentials Credentials;
string CourseNumber;
string CourseID;
dateTime StartDate;
dateTime EndDate;
}"
[4]=>
string(104) "struct irmWebSvcCredentials {
string LogonID;
string Password;
string DataPath;
string DatabaseID;
}"
After reading the answer from: How to make a PHP SOAP call using the SoapClient class
I have attempted the following:
class irmWebSvcCredentials {
public function __construct() {
$this->LogonID = "SomeLogin";
$this->Password = "SomPass";
$this->DataPath = "SomePath";
$this->DatabaseID = "SomeId";
}
}
try {
$wsdlUrl = 'https://irm.cooperboating.com/rdpwincentralsvc/irmpublic.asmx?WSDL';
$client = new SoapClient($wsdlUrl);
$credentials = new irmWebSvcCredentials();
$params = array(
"Credentials" => $credentials,
"CourseNumber" => "",
"CourseID" => "",
"StartDate" => "2019-12-05T18:13:00",
"EndDate" => "2025-12-29T18:13:00",
);
$response = $client->GetCourseInformation(array($params));
var_dump($response);
}
catch(Exception $e) {
echo $e->getMessage();
}
I've also tried inputting "Credentials" as just an array instead of a class as some other answers have suggested like so:
$params = array(
"Credentials" => array(
"LogonID" => "SomeLogin",
"Password" => "SomPass",
"DataPath" => "SomePath",
"DatabaseID" => "SomeId",
),
"CourseNumber" => "",
"CourseID" => "",
"StartDate" => "2019-12-05T18:13:00",
"EndDate" => "2025-12-29T18:13:00",
);
It doesn't seem to matter what I input for the parameters when I call $client->GetCourseInformation, as long as I provide a parameter in the structure of an array it always gives me the same output which is:
object(stdClass)#3 (1) {
["GetCourseInformationResult"]=>
object(stdClass)#4 (3) {
["Status"]=>
int(1)
["ErrMsg"]=>
string(47) "GetCourseInformation_irmRQ is not instantiated."
...
Using Postman I've been able to get the expected output so that leads me to believe that I am not providing a certain parameter. Lastly here is the body I provide in Postman to get the expected output with content type being text/xml:
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetCourseInformation xmlns="http://someurl.com/irmpublic">
<RQ>
<Credentials>
<LogonID>SomeLogin</LogonID>
<Password>SomPass</Password>
<DataPath>SomePath</DataPath>
<DatabaseID>SomeId</DatabaseID>
</Credentials>
<CourseNumber></CourseNumber>
<CourseID></CourseID>
<StartDate>2019-12-20T18:13:00</StartDate>
<EndDate>2025-12-29T18:13:00</EndDate>
</RQ>
</GetCourseInformation>
</soap:Body>
</soap:Envelope>
Is there something I'm not providing? Or does this problem have something to do with the API itself?

This question has been solved. The answer was in the body provided in Postman.
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetCourseInformation xmlns="http://someurl.com/irmpublic">
<RQ> <----- Notice the RQ here
<Credentials>
<LogonID>SomeLogin</LogonID>
<Password>SomPass</Password>
<DataPath>SomePath</DataPath>
<DatabaseID>SomeId</DatabaseID>
</Credentials>
<CourseNumber></CourseNumber>
<CourseID></CourseID>
<StartDate>2019-12-20T18:13:00</StartDate>
<EndDate>2025-12-29T18:13:00</EndDate>
</RQ>
</GetCourseInformation>
</soap:Body>
</soap:Envelope>
The RQ was never provided so it didn't know how to read the provided parameter. To fix this we simply have to change this:
$params = array(
"Credentials" => array(
"LogonID" => "SomeLogin",
"Password" => "SomPass",
"DataPath" => "SomePath",
"DatabaseID" => "SomeId",
),
"CourseNumber" => "",
"CourseID" => "",
"StartDate" => "2019-12-05T18:13:00",
"EndDate" => "2025-12-29T18:13:00",
);
To this:
$params = array(
"RQ" => array(
"Credentials" => array(
"LogonID" => "SomeLogin",
"Password" => "SomPass",
"DataPath" => "SomePath",
"DatabaseID" => "SomeId",
),
"CourseNumber" => "",
"CourseID" => "",
"StartDate" => "2019-12-05T18:13:00",
"EndDate" => "2025-12-29T18:13:00",
)
);
This was a very specific question but I hope this helps someone in the future.

Related

Why the SOAP request is empty?

I develop the system to export some data from the client's side using the SOAP. I have a link to their staging wsdl, and implemented some kind of the SOAP client, but unfortunately my SOAP request is empty and the response is the error one.
Link to WSDL: https://rewardsservices.griris.net/mapi/OrderManagementServices.svc?wsdl
Operation called: exportPendingOrder
Snippet of my SOAP Client:
$soap = new \SoapClient('https://rewardsservices.griris.net/mapi/OrderManagementServices.svc?wsdl', [
'soap_version' => SOAP_1_2,
'cache_wsdl' => WSDL_CACHE_NONE,
'trace' => 1,
'exception' => 1,
]);
​
$headers = [
new SoapHeader(
'http://www.w3.org/2005/08/addressing',
'Action',
'http://tempuri.org/IOrderManagementServices/exportPendingOrder',
true
),
new SoapHeader(
'http://www.w3.org/2005/08/addressing',
'To',
'https://rewardsservices.griris.net/mapi/OrderManagementServices.svc',
true
),
];
​
$soap->__setSoapHeaders($headers);
​
try {
$params = [
'parameters' => [
'merchantNetworkID' => "XXX",
'merchantCode' => "XXX",
'subProgramNetworkID' => "XXX",
'countryISOCode' => "XXX",
'grToken' => "XXX",
'requestId' => (new \DateTime())->getTimestamp(),
],
];
​
$result = $soap->exportPendingOrder($params);
​
var_dump([
'params' => $params,
'result' => $result,
'request' => $soap->__getLastRequest(),
'response' => $soap->__getLastResponse(),
]);
} catch (\SoapFault $exception) {
var_dump([
'error_message' => $exception->getMessage(),
'request' => $soap->__getLastRequest(),
'response' => $soap->__getLastResponse(),
]);
}
Log information (incl. the request/response):
array(4) {
["params"]=>
array(1) {
["parameters"]=>
array(6) {
["merchantNetworkID"]=>
string(36) "XXX"
["merchantCode"]=>
string(3) "XXX"
["subProgramNetworkID"]=>
string(36) "XXX"
["countryISOCode"]=>
string(2) "XXX"
["grToken"]=>
string(110) "XXX"
["requestId"]=>
int(1619772724)
}
}
["result"]=>
object(stdClass)#185 (1) {
["exportPendingOrderResult"]=>
string(121) "{"responseCode":"1002","description":"Required field value missing","result":{"requestID":null,"serializedDataset":null}}"
}
["request"]=>
string(496) "<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://tempuri.org/" xmlns:ns2="http://www.w3.org/2005/08/addressing"><env:Header><ns2:Action env:mustUnderstand="true">http://tempuri.org/IOrderManagementServices/exportPendingOrder</ns2:Action><ns2:To env:mustUnderstand="true">https://rewardsservices.griris.net/mapi/OrderManagementServices.svc</ns2:To></env:Header><env:Body><ns1:exportPendingOrder/></env:Body></env:Envelope>
"
["response"]=>
string(531) "<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"><s:Header><a:Action s:mustUnderstand="1">http://tempuri.org/IOrderManagementServices/exportPendingOrderResponse</a:Action></s:Header><s:Body><exportPendingOrderResponse xmlns="http://tempuri.org/"><exportPendingOrderResult>{"responseCode":"1002","description":"Required field value missing","result":{"requestID":null,"serializedDataset":null}}</exportPendingOrderResult></exportPendingOrderResponse></s:Body></s:Envelope>"
}
Could you please advise what I do wrongly, and why my SOAP request is empty basing on the wsdl provided? Any help is appreciated!
Thanks in advance,
Yevhen
Finally I have managed to send the non-empty request. I have checked the partner's wsdl using the SoapUI tool and it showed me the correct format of the request. So the correct request has to be the following one:
...
$params = [
'JsonData' => json_encode([
'merchantNetworkID' => "XXX",
'merchantCode' => "XXX",
'subProgramNetworkID' => "XXX",
'countryISOCode' => "XXX",
'grToken' => "XXX",
'requestId' => (new \DateTime())->getTimestamp(),
]),
];
...

SOAP call PHP not sending all parameters

I am trying to make a SOAP call in PHP while I am trying to pass some parameters some of them are never sent. I do not really know if it is a problem about building the LoginYInsertarEvento2Params object or it is more related about the soap call.
This is the object that I build for making the call
$LoginYInsertarEvento2Params = array(
"SystemUser" => "XXXXX",
"Password" => "XXXXXX",
"Dominio" => $registration,
"NroSerie" => $terminal_serial,
"Codigo" => "",
"Latitud" => $latitude,
"Longitud" => $longitude,
"Altitud" => "0",
"Velocidad" => $speed,
"FechaHoraEvento" => date('c', strtotime($fecha_evento)),
"FechaHoraRecepcion" => date('c', strtotime($fecha_registro)),
"Valido" => true,
"Sensores" =>array ( ["Temperatura" => $temp], ["Luminosidad" => $lum] )
);
This the request:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="XXXXXXXXXXXXXXXXXXX">
<SOAP-ENV:Body>
<ns1:LoginYInsertarEvento2>
<ns1:SystemUser>XXXXX</ns1:SystemUser>
<ns1:Password>XXXXX</ns1:Password>
<ns1:Dominio>XXXXXX</ns1:Dominio>
<ns1:NroSerie>-1</ns1:NroSerie>
<ns1:Codigo></ns1:Codigo>
<ns1:Latitud>40.348054</ns1:Latitud>
<ns1:Longitud>-3.681576</ns1:Longitud>
<ns1:Altitud>0</ns1:Altitud>
<ns1:Velocidad>0</ns1:Velocidad>
<ns1:FechaHoraEvento>2020-10-09T12:50:40+02:00</ns1:FechaHoraEvento>
<ns1:FechaHoraRecepcion>2020-10-09T12:50:56+02:00</ns1:FechaHoraRecepcion>
<ns1:Valido>true</ns1:Valido>
<ns1:Sensores>
<ns1:pSensor/>
<ns1:pSensor/>
</ns1:Sensores>
</ns1:LoginYInsertarEvento2>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
And it should be like this one...
<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<LoginYInsertarEvento2 xmlns="http://unisolutions.com.ar/">
<SystemUser>XXXXXX</SystemUser>
<Password>xxxxxxxx</Password>
<Dominio>XXXXXXX</Dominio>
<NroSerie>-1</NroSerie>
<Codigo />
<Latitud>41.748311</Latitud>
<Longitud>-0.848768</Longitud>
<Altitud>274</Altitud>
<Velocidad>0</Velocidad>
<FechaHoraEvento>2020-10-06T09:36:56</FechaHoraEvento>
<FechaHoraRecepcion>2020-10-06T09:36:56</FechaHoraRecepcion>
<Valido>true</Valido>
<Sensores>
<pSensor>
<Clave>Temperatura</Clave>
<Valor>11.6</Valor>
</pSensor>
<pSensor>
<Clave>Luminosidad</Clave>
<Valor>0</Valor>
</pSensor>
</Sensores>
</LoginYInsertarEvento2>
</s:Body>
</s:Envelope>
Thanks guys!
Judging by what you expect your output to be. Try changing the structure to look like this:
$LoginYInsertarEvento2Params = array(
"SystemUser" => "XXXXX",
"Password" => "XXXXXX",
"Dominio" => $registration,
"NroSerie" => $terminal_serial,
"Codigo" => "",
"Latitud" => $latitude,
"Longitud" => $longitude,
"Altitud" => "0",
"Velocidad" => $speed,
"FechaHoraEvento" => date('c', strtotime($fecha_evento)),
"FechaHoraRecepcion" => date('c', strtotime($fecha_registro)),
"Valido" => true,
"Sensores" =>array (
["Clave" => "Temperatura", "Valor" => $temp],
["Clave" => "Luminosidad", "Valor" => $lum]
)
);
I may be wrong, but it just looks like the Sensores structure is incorrect.

PHP SOAP not including my parameters in request

I'm trying to access the HelloWorldCredentials service on
https://statistik.uni-c.dk/instregws/DataServiceXML.asmx?op=HelloWorldCredentials
I have the necessary credentials.
As far as I can see, I need to submit an array called "Credentials" containing one array containing two strings, one called "Username" and one called "Password".
I build my array like this:
$params = array(
"Credentials" => array(
"Username" => "Obviously",
"Password" => "NotPublic",
)
);
However, when I execute
$client = new SoapClient("https://statistik.uni-c.dk/instregws/DataServiceXML.asmx?wsdl", array('trace' => 1));
$params = array(
"Credentials" => array(
"Username" => "Obviously",
"Password" => "NotPublic",
)
);
$response = $client->__soapCall("HelloWorldCredentials", array($params));
echo("*** PARAMS ***\n");
var_dump( $params );
echo("\n*** REQUEST ***\n");
echo( $client->__getLastRequest() );
echo("\n*** RESPONSE ***\n");
var_dump( $response );
I get
*** PARAMS ***
array(1) {
["Credentials"]=>
array(2) {
["Username"]=>
string(11) "Obviously"
["Password"]=>
string(8) "NotPublic"
}
}
-as I should, but
*** REQUEST ***
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://statistik.uni-c.dk/instreg/">
<SOAP-ENV:Body>
<ns1:HelloWorldCredentials/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
So obviously I get
*** RESPONSE ***
object(stdClass)#3 (1) {
["HelloWorldCredentialsResult"]=>
string(19) "Missing credentials"
}
Why are my parameters completely absent from the request?
according to the WSDL for the HelloWorldCredentials method you need to send the Credentials in the Header not the Body of the soap Envelope
so this should work:
<?php
$client = new SoapClient("https://statistik.uni-c.dk/instregws/DataServiceXML.asmx?wsdl", array('trace' => 1));
$credentials = array(
'Username' => 'Obviously',
'Password' => 'NotPublic'
);
$header = new SoapHeader('http://statistik.uni-c.dk/instreg/', 'Credentials', $credentials);
$client->__setSoapHeaders($header);
$response = $client->HelloWorldCredentials();
echo("\n*** REQUEST ***\n");
echo( $client->__getLastRequest() );
echo("\n*** RESPONSE ***\n");
var_dump( $response );

I call a function with cURL and get a response but when i call the exact same function with soapclient i get a fault

What do I need:
I need some help figuring out an error that i receive from calling a function from a WSDL file with soapclient. Or I would like some help extracting data from a cURL response.
What am I trying to reach:
I am trying to reach to call a function from a WSDL, and get a response from the action.
What are my experiences:
I can call the actions storeOrders succesfull with a cURL statement, I do also get a response.
But with the given response i guess a string. I am not able to extract the data out of it.
So I tried to request the same action from the server but then using soapclient, but I keep getting a error.
What I already tried:
I tried to make the cURL response a new SimpleXMLElement, but it always returns a emty object. Also when I try to reach one of the children.
I tried to make the cURL reponse return as an array and loop trough it with a foreach, also here I got an empty object.
I tried to explode the cURL reponse, but also there i had some problems with the wrong data being returned.
I tried to call it with SoapClient, but I keep getting this error.
So I would like some help with extracting data from cURL, or processing the request with SoapClient.
My cURL request (with answer, all the variables are set with the correct data):
function storeOrderAndGetLabel($delisId, $auth_token, $messageLanguage, $printerLanguage, $paperFormat, $identificationNumber,
$sendingDepot, $product, $mpsCompleteDelivery, $send_name, $send_street, $send_country, $send_zipcode, $send_city,
$send_customerNumber, $rec_name, $rec_street, $rec_state, $rec_country, $rec_zipcode, $rec_city, $parcelLabelNumber,
$orderType)
{
$xml = '
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://dpd.com/common/service/types/Authentication/2.0" xmlns:ns1="http://dpd.com/common/service/types/ShipmentService/3.1">
<soapenv:Header>
<ns:authentication>
<delisId>'.$delisId.'</delisId>
<authToken>'.$auth_token.'</authToken>
<messageLanguage>'.$messageLanguage.'</messageLanguage>
</ns:authentication>
</soapenv:Header>
<soapenv:Body>
<ns1:storeOrders>
<printOptions>
<printerLanguage>'.$printerLanguage.'</printerLanguage>
<paperFormat>'.$paperFormat.'</paperFormat>
</printOptions>
<order>
<generalShipmentData>
<identificationNumber>'.$identificationNumber.'</identificationNumber>
<sendingDepot>'.$sendingDepot.'</sendingDepot>
<product>'.$product.'</product>
<mpsCompleteDelivery>'.$mpsCompleteDelivery.'</mpsCompleteDelivery>
<sender>
<name1>'.$send_name.'</name1>
<street>'.$send_street.'</street>
<country>'.$send_country.'</country>
<zipCode>'.$send_zipcode.'</zipCode>
<city>'.$send_city.'</city>
<customerNumber>'.$send_customerNumber.'</customerNumber>
</sender>
<recipient>
<name1>'.$rec_name.'</name1>
<street>'.$rec_street.'</street>
<state>'.$rec_state.'</state>
<country>'.$rec_country.'</country>
<zipCode>'.$rec_zipcode.'</zipCode>
<city>'.$rec_city.'</city>
</recipient>
</generalShipmentData>
<parcels>
<parcelLabelNumber>'.$parcelLabelNumber.'</parcelLabelNumber>
</parcels>
<productAndServiceData>
<orderType>'.$orderType.'</orderType>
</productAndServiceData>
</order>
</ns1:storeOrders>
</soapenv:Body>
</soapenv:Envelope>
';
$headers = array(
"POST HTTP/1.1",
"Content-type: application/soap+xml; charset=\"utf-8\"",
"SOAPAction: \"http://dpd.com/common/service/ShipmentService/3.1/storeOrders\"",
"Content-length: ".strlen($xml)
);
$cl = curl_init('https://public-ws-stage.dpd.com/services/ShipmentService/V3_1/');
curl_setopt($cl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($cl, CURLOPT_POST, 1);
curl_setopt($cl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($cl, CURLOPT_POSTFIELDS, "$xml");
curl_setopt($cl, CURLOPT_RETURNTRANSFER, 1);
$output_cl = json_decode(trim(json_encode(curl_exec($cl))), TRUE);
return $output_cl;
//return $output_cl;
}
And from this code i get the reponse, i guess it is a string but i don't know for sure:
<soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:body>
<ns2:storeordersresponse xmlns:ns2="http://dpd.com/common/service/types/ShipmentService/3.1">
<orderresult>
<parcellabelspdf>pdfkey</parcellabelspdf>
<shipmentresponses>
<identificationnumber>identificationnumber</identificationnumber>
<mpsid>mpsid</mpsid>
<parcelinformation>
<parcellabelnumber>labelnr</parcellabelnumber>
</parcelinformation>
</shipmentresponses>
</orderresult>
</ns2:storeordersresponse>
</soap:body>
</soap:envelope>
Now my function calling the SoapClient function:
$label = storeOrderAndGetLabel($delisId, $auth_token, $messageLanguage, $printerLanguage, $paperFormat, $identificationNumber,
$sedingDepot, $product, $mpsCompleteDelivery, $send_name, $send_street, $send_country, $send_zipcode, $send_city,
$send_customerNumber, $rec_name, $rec_street, $rec_state, $rec_country, $rec_zipcode, $rec_city, $parcelLabelNumber,
$orderType);
print_r($label);
now the soap call itself:
function storeOrderAndGetLabel($delisId, $auth_token, $messageLanguage, $printerLanguage, $paperFormat, $identificationNumber,
$sendingDepot, $product, $mpsCompleteDelivery, $send_name, $send_street, $send_country, $send_zipcode, $send_city,
$send_customerNumber, $rec_name, $rec_street, $rec_state, $rec_country, $rec_zipcode, $rec_city, $parcelLabelNumber,
$orderType)
{
$client = new SoapClient('https://public-ws-stage.dpd.com/services/ShipmentService/V3_1?WSDL');
$label = $client->storeOrders
(array
(
"printOptions" => array
(
"printerLanguage" => "$printerLanguage",
"paperFormat" => "$paperFormat"
),
"order" => array
(
"generalShipmentData" => array
(
"identificationNumber" => "$identificationNumber",
"sendingDepot" => "$sendingDepot",
"product" => "$product",
"mpsCompleteDelivery" => "$mpsCompleteDelivery",
"sender" => array
(
"name1" => "$send_name",
"street" => "$send_street",
"country" => "$send_country",
"zipCode" => "$send_zipcode",
"city" => "$send_city",
"customerNumber" => "$send_customerNumber"
),
"recipient" => array
(
"name1" => "$rec_name",
"street" => "$rec_street",
"state" => "$rec_state",
"country" => "$rec_country",
"zipCode" => "$rec_zipcode",
"city" => "$rec_city"
)
),
"parcels" => array
(
"parcelLabelNumber" => "$parcelLabelNumber"
),
"productAndServiceData" => array
(
"orderType" => "$orderType"
)
)
)
);
return $label;
}
The error I receive from the soapcall:
Fatal error: Uncaught SoapFault exception: [soap:Server] Fault occurred while processing. in getLabel.php:107 Stack trace: #0 getLabel.php(107): SoapClient->__call('storeOrders', Array) #1 getLabel.php(107): SoapClient->storeOrders(Array) #2 getLabel.php(38): storeOrderAndGetLabel('username', 'password...', 'nl_NL', 'PDF', 'A4', '77777', '0163', 'CL', '0', 'uname', 'straat', 'NL', 'zipcode', 'City', '341546246451...', 'Test-Empfaenger', 'Test-Strasse', 'BY', 'DE', '123451', 'ahahaha', '16231545', 'consignment') #3 {main} thrown in getLabel.php on line 107
I would like to extract the parcellabelspdf key and the mpsid from the response. It would be really nice if someone could take a look at it.
Two possible problems:
You need to authenticate when calling the DPD ShipmentService. See below for a working example.
Make sure, that the parameter mpsCompleteDelivery is passed as an integer (0), not the string "false". Consider changing this line:
"mpsCompleteDelivery" => "$mpsCompleteDelivery"
to:
"mpsCompleteDelivery" => $mpsCompleteDelivery
Here is a full example including the login and output of a DPD-label as PDF:
// Let's log in first...
$c = new SoapClient('https://public-ws-stage.dpd.com/services/LoginService/V2_0?wsdl');
$res = $c->getAuth(array(
'delisId' => 'your-Id',
'password' => 'your-Password',
'messageLanguage' => 'de_DE'
));
// ...and remember the token.
$auth = $res->return;
// ...and then generate a label
$c = new SoapClient('https://public-ws-stage.dpd.com/services/ShipmentService/V3_1?wsdl');
$token = array(
'delisId' => $auth->delisId,
'authToken' => $auth->authToken,
'messageLanguage' => 'de_DE'
);
// Set the header with the authentication token
$header = new SOAPHeader('http://dpd.com/common/service/types/Authentication/2.0', 'authentication', $token);
$c->__setSoapHeaders($header);
try {
$res = $c->storeOrders( array
(
"printOptions" => array(
"paperFormat" => "A4",
"printerLanguage" => "PDF"
),
"order" => array(
"generalShipmentData" => array(
"sendingDepot" => $auth->depot,
"product" => "CL",
"mpsCompleteDelivery" => false,
"sender" => array(
"name1" => "Sender Name",
"street" => "Sender Street 2",
"country" => "DE",
"zipCode" => "65189",
"city" => "Wiesbaden",
"customerNumber" => "123456789"
),
"recipient" => array(
"name1" => "John Malone",
"street" => "Johns Street 34",
"country" => "DE",
"zipCode" => "65201",
"city" => "Wiesbaden"
)
),
"parcels" => array(
"parcelLabelNumber" => "09123829120"
),
"productAndServiceData" => array(
"orderType" => "consignment"
)
)
)
);
} catch (SoapFault $exception) {
echo $exception->getMessage();
die();
}
// Et voilà!
header('Content-type: application/pdf');
echo $res->orderResult->parcellabelsPDF;
Check here for more information:
http://labor.99grad.de/2014/10/05/deutscher-paket-dienst-dpd-soap-schnittstelle-mit-php-nutzen-um-versandetikett-als-pdf-zu-generieren/

Returning multiple values via SOAP in PHP

tl;dr: sending multiple strings as a response to SOAP request.
I am new to SOAP. I have written a simple web service which serves request via SOAP. As I wanted to implement this in PHP, I have used NuSOAP library. The specification given to me for the SOAP API design is as follows.
REQUEST FORMAT:
<?xml version="1.0" encoding="utf-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://www.sandeepraju.in/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<soapenv:Body> 
<q0:getData> 
<token>String</token> 
</q0:getData> 
</soapenv:Body> 
</soapenv:Envelope>
EXAMPLE / SAMPLE RESPONSE:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getDataResponse xmlns:ns2="http://www.sandeepraju.in/">
<return>
<Data>
<animal>cat</animal>
<phone>225</phone>
<code>FYI</code>
</Data>
The PHP code I have written for the above specification is as follows.
require_once("../nusoap_old/lib/nusoap.php");
// Definition of getData operation
function getData($token) {
if($token == "somestring") {
return array("animal" => "cat", "phone" => "225", "code" => "FYI");
}
else {
return array("animal" => "null", "phone" => "null", "code" => "null");
}
}
// Creating SOAP server Object
$server = new soap_server();
// Setup WSDL
$server->configureWSDL('catnews', 'urn:catinfo');
$server->wsdl->addComplexType('return_array_php',
'complexType',
'struct',
'all',
'',
array(
'animal' => array('animal' => 'animal', 'type' => 'xsd:string'),
'phone' => array('phone' => 'phone', 'type' => 'xsd:string'),
'code' => array('code' => 'code', 'type' => 'xsd:string')
)
);
// Register the getData operation
$server->register("getData",
array('token' => 'xsd:string'),
array('return' => 'tns:return_array_php'),
'urn:catinfo',
'urn:catinfo#getData');
// Checking POST request headers
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA)? $HTTP_RAW_POST_DATA : "";
$server->service($HTTP_RAW_POST_DATA);
Here, I think I should NOT return a PHP array. But I am not sure what I should return according the specification. Can anyone help me with this. Or returning an array is correct?
You need to add another complex type for an array consisting of your data.
Like this:
$server->wsdl->addComplexType(
'dataArray', // MySoapObjectArray
'complexType', 'array', '', 'SOAP-ENC:Array',
array(),
array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:return_array_php[]')), 'tns:return_array_php'
);
Register the new datatype to be the return-value of the function.
$server->register(
'getData',
array('Datum'=>'xsd:token'),
array('return'=>'tns:dataArray'),
$namespace,
false,
'rpc',
'encoded',
'description'
);
Then your function needs to set the single parts of the array.
function GetData($token)
{
if($token == "somestring") {
$result[0] = array(
"animal" => "cat",
"phone" => "225",
"code" => "FYI"
);
$result[1] = array(
"animal" => "dog",
"phone" => "552",
"code" => "IFY"
);
} else {
$result = null;
}
return $result;
}
The response of this service called with the string "somestring" will be:
<ns1:getDataResponse xmlns:ns1="http://localhost/status/status.php">
<return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:return_array_php[2]">
<item xsi:type="tns:return_array_php">
<animal xsi:type="xsd:string">cat</animal>
<phone xsi:type="xsd:string">225</phone>
<code xsi:type="xsd:string">FYI</code>
</item>
<item xsi:type="tns:return_array_php">
<animal xsi:type="xsd:string">dog</animal>
<phone xsi:type="xsd:string">552</phone>
<code xsi:type="xsd:string">IFY</code>
</item>
</return>
</ns1:getDataResponse>
That matches your specifications.

Categories