PHP SOAP is not buildung a correct request - php

i am trying to build a specific SOAP Request in PHP with the standard SOAP-Class.
That's the Request i want to build in PHP:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.autoscout24.com/webapi/" xmlns:data="http://www.autoscout24.com/webapi/data/">
<soapenv:Header/>
<soapenv:Body>
<web:FindArticles>
<web:request>
<data:client_id>1234</data:client_id>
<data:culture_id>de-DE</data:culture_id>
<data:profile_id>CPCMS_DE</data:profile_id>
<data:revision>3</data:revision>
<data:vehicle_search_parameters>
<data:address>
<data:countries>
<data:country_id>D</data:country_id>
</data:countries>
</data:address>
<data:dealer_id>2141356852</data:dealer_id>
</data:vehicle_search_parameters>
</web:request>
</web:FindArticles>
</soapenv:Body>
</soapenv:Envelope>
I tried to figure out how it works:
try {
$result = $client->FindArticles(
array( new SoapParam((string)"de-DE", "culture_id") )
);
} catch (SoapFault $exc) {
var_dump($client->__getLastRequest());
echo $exc->getMessage();
}
This PHP Call echoes the following SOAP-Envelope:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.autoscout24.com/webapi/"><SOAP-ENV:Body>
<ns1:FindArticles/>
</SOAP-ENV:Body></SOAP-ENV:Envelope>
As you can see, PHP ignores my SOAPParameters.
Why is that and how to fix it?
Thanks in Advance!
Florian

I haven't used soapparam yet, I always do it this way:
$arrCallParams = array();
$arrCallParams['culture_id']='de-DE';
$result= $client->FindArticles($arrCallParams);

Related

How to convert Soap xml response return by the Amadeus Flight Search API to json

I would like to convert the SOAP XML response into the JSON format. Can anybody please help!
I got below SOAP XML in $response
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:awsse="http://xml.amadeus.com/2010/06/Session_v3" xmlns:wsa="http://www.w3.org/2005/08/addressing">
<soapenv:Header>
<wsa:To>http://www.w3.org/2005/08/addressing/anonymous</wsa:To>
<wsa:From>
<wsa:Address>https://nodeD2.test.webservices.amadeus.com/XXXXXXXX</wsa:Address>
</wsa:From>
<wsa:Action>http://webservices.amadeus.com/FMPTBQ_18_1_1A
</wsa:Action>
<wsa:MessageID>urn:uuid:a4844bc6-2e05-e8e4-b589-6f813ecad262</wsa:MessageID>
<wsa:RelatesTo RelationshipType="http://www.w3.org/2005/08/addressing/reply">urn:uuid:ac1c8436-ecaf-4bbb-a9ff-11b466dffe18</wsa:RelatesTo>
<awsse:Session TransactionStatusCode="End"><awsse:SessionId>00896YTRZQPQ</awsse:SessionId><awsse:SequenceNumber>1</awsse:SequenceNumber><awsse:SecurityToken>TRFTGYNBHUYKIOL</awsse:SecurityToken></awsse:Session>
</soapenv:Header>
<soapenv:Body>
<Fare_MasterPricerTravelBoardSearchReply xmlns="http://xml.amadeus.com/FMPTBR_18_1_1A">
<replyStatus><status><advisoryTypeInfo>FQX</advisoryTypeInfo></status></replyStatus>
<conversionRate><conversionRateDetail><currency>USD</currency></conversionRateDetail></conversionRate>
<familyInformation><refNumber>1</refNumber><fareFamilyname>BAGSEAT</fareFamilyname><description>OPTIMA</description><carrier>IB</carrier><services><reference>1</reference><status>CHA</status></services><services><reference>2</reference><status>CHA</status></services><services><reference>3</reference><status>INC</status></services><services><reference>4</reference><status>NOF</status></services><services><reference>5</reference><status>NOF</status></services><services><reference>6</reference><status>NOF</status></services><services><reference>7</reference><status>NOF</status></services></familyInformation>
</Fare_MasterPricerTravelBoardSearchReply>
</soapenv:Body>
</soapenv:Envelope>
I tried this.
$xml = json_decode(json_encode(simplexml_load_string($response)), true);
print '<pre>';
print_r($xml);
print '</pre>';
I got the solution to the problem, working fine
$response = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $response);
$response = simplexml_load_string($response);
$dataArray = json_decode(json_encode((array)$response), TRUE);
echo '<pre>';print_r($dataArray);echo '</pre>';die(__FILE__.' On this line '.__LINE__);

Soap request working in SoapUI but not in Wordpress plugin

I've been searching around for a solution to my problem, but to no avail. I'm trying to send a soap request through a Wordpress plugin using the following:
function soapRequest($soapUsername, $soapNonce, $soapDateTime, $soapPassword) {
$wsdl = 'http://www.beautyfort.com/api/wsdl/v2/wsdl.wsdl';
$trace = true;
$exceptions = false;
$client = new SoapClient($wsdl, array('trace' => $trace, 'exceptions' => $exceptions));
// Must be a stdClass (and not an array)
$auth = new stdClass();
$auth->Username = $soapUsername;
$auth->Nonce = $soapNonce;
$auth->Created = $soapDateTime;
$auth->Password = $soapPassword;
$header = new SoapHeader('http://www.beautyfort.com/api/', 'AuthHeader', $auth);
$client->__setSoapHeaders($header);
$xml_array['TestMode'] = 'true';
$xml_array['StockFileFormat'] = 'JSON';
$xml_array['SortBy'] = 'StockCode';
try {
$response = $client->GetStockFile($xml_array);
}
catch (Exception $e) {
log_me("Error!");
log_me($e -> getMessage());
log_me('Last response: '. $client->__getLastResponse());
}
log_me('Last request: '. $client->__getLastRequest());
log_me($response);
}
This produces the following request:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://www.beautyfort.com/api/">
<SOAP-ENV:Header>
<ns1:AuthHeader>
<ns1:Username>joetest</ns1:Username>
<ns1:Nonce>htflFfIKM4</ns1:Nonce>
<ns1:Created>2019-02-09T10:13:51.000Z</ns1:Created>
<ns1:Password>NGFjYTJiNzJmOWY2MzBmY2M2MjJkNjg1MDgyMWRjMzQxOGY1YTNjYQ==</ns1:Password>
</ns1:AuthHeader>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ns1:GetStockFileRequest>
<ns1:TestMode>true</ns1:TestMode>
<ns1:StockFileFormat>JSON</ns1:StockFileFormat>
<ns1:SortBy>StockCode</ns1:SortBy>
</ns1:GetStockFileRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
And I get an invalid credentials error. I've also been testing in SoupUI and the following request works:
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:api="http://www.beautyfort.com/api/">
<soapenv:Header>
<api:AuthHeader>
<api:Username>joetest</api:Username>
<api:Nonce>tJrsRlQt6i</api:Nonce>
<api:Created>2019-02-06T23:34:11.000Z</api:Created>
<api:Password>ZTBhMmE5OGY4YTNlZWIzZTE0ZTc2ZjZiZDBhM2RhMjJmNzAxNzYwZA==</api:Password>
</api:AuthHeader>
</soapenv:Header>
<soapenv:Body>
<api:GetStockFileRequest>
<api:TestMode>true</api:TestMode>
<api:StockFileFormat>JSON</api:StockFileFormat>
<!--Optional:-->
<api:FieldDelimiter>,</api:FieldDelimiter>
<!--Optional:-->
<api:StockFileFields>
<!--1 or more repetitions:-->
<api:StockFileField>StockCode</api:StockFileField>
<api:StockFileField>Category</api:StockFileField>
<api:StockFileField>Brand</api:StockFileField>
<api:StockFileField>Collection</api:StockFileField>
<api:StockFileField>Category</api:StockFileField>
</api:StockFileFields>
<api:SortBy>StockCode</api:SortBy>
</api:GetStockFileRequest>
</soapenv:Body>
</soapenv:Envelope>
Now the only differences I can see (apart from the optional fields) is the names of the namespace, and the use of the Xml tag at the top of the request. Both of these shouldn't matter right? I'd really appreciate your help on this as I've been scratching my head for ages.
Thank you in advance!
Your perfect just need to set UTC timezone and secret format like below:
base64 encoded(sha1(Nonce . Created . Secret))

SOAP request with soapenv

I have the following code for connecting to a soap client. I am struggling hard to find how to create a proper soap request.
$soap = new SoapClient($apiWsdl,
array("soap_version" => SOAP_1_1,
"trace" => 1));
var_dump($soap);
echo $soap->__getLastResponse();
try{
$data = $soap->login($apiUser,$apiKey);
}
catch (SoapFault $soapFault) {
echo "Request :<br>", htmlentities($soap->__getLastRequest()), "<br>";
echo "Response :<br>", htmlentities($soap->__getLastResponse()), "<br>";
}
I get the following XML built:
<?xml version="1.0" encoding="UTF-8"?> \<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:login><username xsi:type="xsd:string">XXXXXXX</username><apiKey xsi:type="xsd:string">XXXXXXXX</apiKey></ns1:login></SOAP-ENV:Body></SOAP-ENV:Envelope>
Which returns be a forbidden access.
However the server is accepting only request of the following form
<soapenv:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:urn="urn:Magento">
<soapenv:Header/>
<soapenv:Body>
<urn:login soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<username xsi:type="xsd:string">XXXXXXX</username>
<apiKey xsi:type="xsd:string">XXXXXX</apiKey>
</urn:login>
</soapenv:Body>
</soapenv:Envelope>
Can somebody please let me know what mistake I am making in the code.
It looks like you have a typo right after the XML tags. There is a backslash that should not be there. This could trigger your errors.
HTH, Jim

Soap PHP : double XML tag

Hye
I'm trying to use a WebService with SOAP in PHP. But the SoapVar creates my DOM XML with double XML tag
My code :
$xmlContent = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:com="http://schemas.datacontract.org/2004/07/Commun.Metier">
<soapenv:Header/>
<soapenv:Body>
<tem:SendData> ....';
$soapClient = new SoapClient("URL OF THE WSDL", array('trace' => 1, 'location' => 'URL OF THE SERVICE', 'uri' => 'URL OF THE SERVICE'));
$myXML = new SoapVar($xmlContent, XSD_ANYXML);
try {
$result = $soapClient->__soapCall("SendData", array('SendData' => $myXML));
} catch (Exception $e) {
var_dump($e);
}
The problem is that I have an exception Bad request because the XML I'm sending haves 2 XML tags and Soap Enveloppe :
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
<SOAP-ENV:Body><?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:com="http://schemas.datacontract.org/2004/07/Commun.Metier">
<soapenv:Header/>
<soapenv:Body>
<tem:SendData>
How can I cancel this double tag ?
Thanks

PHP SOAP client not sending parameters correctly. xsi:nil="true" instead of value

I am trying to consume a web service with no success. It looks like the xml is no properly set.
This is my PHP code:
<?php
$wsdlUrl = "http://api.clickatell.com/soap/webservice.php?WSDL";
$serviceUrl = "http://api.clickatell.com/soap/webservice.php";
$request = array("user"=>"anyuser",
"password"=>"asdsda",
"api_id"=>"1234"
);
var_dump($request);
try {
$client = new SoapClient($wsdlUrl, array("trace" => 1, "location" => $serviceUrl));
var_dump($client);
echo "\n";
$response = $client->auth($request);
var_dump($response);
var_dump($client->__getLastRequest());
echo "\n";
}
catch(Exception $exp) {
echo "EXCEPTION";
}
?>
And the SOAP packet is:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://api.clickatell.com/soap/webservice" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:auth>
<api_id xsi:type="xsd:int">1</api_id>
<user xsi:nil="true"/>
<password xsi:nil="true"/>
</ns1:auth>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
What is wrong with the code that causes that api_id, user and password are not being sent?
If you check out var_dump($client->__getFunctions()), you can see, that the parameters have to be passed like with a normal function call.
So you could do either the verbose thing with SoapParam:
$response = $client->auth(
new SoapParam($request["api_id"], "api_id"),
new SoapParam($request["user"], "user"),
new SoapParam($request["password"], "password")
);
Or less verbose by just giving the parameters directly:
$response = $client->auth($request["api_id"],
$request["user"], $request["password"]);

Categories