PHP SOAP not including my parameters in request - php

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

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(),
]),
];
...

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

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.

PHP SOAP client not creating body

After over a half a day of trying and reading tutorials on creating a simple SOAP client, I am no closer to retrieving a request from API I attempting to work with.
WSDL: http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL
I can make the request from SOAP UI with the following simple SOAP request:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pub="http://publicapi.ekmpowershop.com/">
<soapenv:Header/>
<soapenv:Body>
<pub:GetOrders>
<!--Optional:-->
<pub:GetOrdersRequest>
<!--Optional:-->
<pub:APIKey>myApiKey</pub:APIKey>
</pub:GetOrdersRequest>
</pub:GetOrders>
</soapenv:Body>
</soapenv:Envelope>
This above returns the expected data.
When it comes to translating the request into a PHP I have the following:
$wsdl = 'http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL';
$trace = true;
$exceptions = false;
$debug = true;
$client = new SoapClient($wsdl,
array(
'trace' => $trace,
'exceptions' => $exceptions,
'debug' => $debug,
));
$param = array('GetOrdersRequest' => array(
'APIKey' => 'myApiKey'
)
);
$resp = $client->GetOrders();
print_r($param);
print_r($client->__getLastRequest());
print_r($client->__getLastResponse());
If put the $param into the GetOrders function then, it breaks and nothing happens.
Even if I use an array in the $client->GetOrders(array('someArry' => $param)) then response and request still always looks the same and looks like the body of the SOAP request is never created:
Request:
?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://publicapi.ekmpowershop.com/"><SOAP-ENV:Body><ns1:GetOrders/></SOAP-ENV:Body></SOAP-ENV:Envelope>
Response:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetOrdersResponse xmlns="http://publicapi.ekmpowershop.com/"><GetOrdersResult><Status>Failure</Status><Errors><string>Object reference not set to an instance of an object.</string></Errors><Date>2017-04-03T16:00:42.9457446+01:00</Date><TotalOrders>0</TotalOrders><TotalCost xsi:nil="true" /></GetOrdersResult></GetOrdersResponse></soap:Body></soap:Envelope>
If anyone can shed some light on what I am doing wrong here that would be real big help?
P.S My experience of SOAP in PHP is limited as I am used to SOAP in a java env. Thanks
You need to pass the parameters into the $client->GetOrders() call. Also the WSDL defines some required parameters, so a minimal example is something like:
$wsdl = 'http://publicapi.ekmpowershop31.com/v1.1/publicapi.asmx?WSDL';
$trace = true;
$exceptions = false;
$debug = true;
$client = new SoapClient($wsdl,
array(
'trace' => $trace,
'exceptions' => $exceptions,
'debug' => $debug,
));
$param = array(
'GetOrdersRequest' => array(
'APIKey' => 'dummy-key',
'CustomerID' => 1,
'ItemsPerPage' => 1,
'PageNumber' => 1,
)
);
$resp = $client->GetOrders($param);
print_r($param);
print_r($client->__getLastRequest());
print_r($client->__getLastResponse());
This gives the error response:
<Errors><string>Invalid character in a Base-64 string.</string></Errors>
which presumably is because my API key is invalid.

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/

PHP SoapClient target namespace not in request params

I am using PHP SoapClient in WSDL mode.
This is what the expected SOAP request should look like:
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns="http://xml.m4u.com.au/2009">
<soapenv:Header/>
<soapenv:Body>
<ns:sendMessages>
<ns:authentication>
<ns:userId>Username</ns:userId>
<ns:password>Password</ns:password>
</ns:authentication>
<ns:requestBody>
<ns:messages>
<ns:message>
<ns:recipients>
<ns:recipient>61400000001</ns:recipient>
</ns:recipients>
<ns:content>Message Content</ns:content>
</ns:message>
</ns:messages>
</ns:requestBody>
</ns:sendMessages>
</soapenv:Body>
</soapenv:Envelope>
And this is what PHP SoapClient is sending:
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://xml.m4u.com.au/2009">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns1:sendMessages>
<ns1:authentication>
<userId>Username</userId>
<password>Password</password>
</ns1:authentication>
<ns1:requestBody>
<messages>
<message>
<recipients>
<recipient>61400000001</recipient>
</recipients>
<content>Message Content</content>
</message>
</messages>
</ns1:requestBody>
</ns1:sendMessages>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
This is how I constructed the client and params:
function sendMessages($recipient, $content) {
$authenticationType = new SoapVar(array('userId' => $this->username, 'password' => $this->password), SOAP_ENC_OBJECT);
$recipientsType = new SoapVar(array('recipient' => $recipient), SOAP_ENC_OBJECT);
$messageType = new SoapVar(array('recipients' => $recipientsType, 'content' => $content), SOAP_ENC_OBJECT);
$messagesType = new SoapVar(array('message' => $messageType), SOAP_ENC_OBJECT);
$requestBodyType = new SoapVar(array('messages' => $messagesType), SOAP_ENC_OBJECT);
$params = array(
'authentication' => $authenticationType,
'requestBody' => $requestBodyType
);
try {
$this->soapClient = new SoapClient($this->wsdl, array('trace' => 1));
$this->soapClient->__setSoapHeaders(array());
return $this->soapClient->sendMessages($params);
} catch (SoapFault $fault) {
echo '<h2>Request</h2><pre>' . htmlspecialchars($this->soapClient->__getLastRequest(), ENT_QUOTES) . '</pre>';
trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
}
}
Why is 'ns1' present for 'authentication' and 'requestBody' but missing for their child nodes?
What am I doing wrong? The WSDL is located here => http://soap.m4u.com.au/?wsdl
Appreciate anyone who can help.
You must specify the URI of the namespace to encode the object. This will include everything necessary (including your missing namespace). The acronym of the namespace name is irrelevant.
This:
$authenticationType = new SoapVar(array('userId' => $this->username, 'password' => $this->password), SOAP_ENC_OBJECT);
should be:
$authenticationType = new SoapVar(array('userId' => $this->username, 'password' => $this->password), SOAP_ENC_OBJECT, "authentication","http://xml.m4u.com.au/2009");
The problem you're seeing with the namespace serialization comes from using soapvar without all the parameters. When it serializes the request prior to sending it assumes the namespace is already included. If, instead, you had created each as a simple array and included them in $params it would include the namespace for the internal parameters correctly. By way of demonstration:
$authenticationType = array('userId' => $this->username, 'password' => $this->password)
try using nusoap library. Below is sample code:
<?php
$wsdl = "http://soap.m4u.com.au/?wsdl";
// generate request veriables
$data = array();
$action = ""; // ws action
$param = ""; //parameters
$options = array(
'location' => 'http://soap.m4u.com.au',
'uri' => ''
);
// eof generate request veriables
//$client = new soap_client($wsdl, $options);// create soap client
$client = new nusoap_client($wsdl, 'wsdl');
$client->setCredentials($api_username, $api_password);// set crendencials
$opt = $client->call($action, $param, '', '', false, true);
print_r($opt);
?>

Categories