I am getting Sabre SOAP API response something like :
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:header></soap-env:header>
<soap-env:body>
<ota_airlowfaresearchrs xmlns="http://www.opentravel.org/OTA/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.1.0" priceditincount="50" brandedonewayitincount="0" simpleonewayitincount="0" departeditincount="0" soldoutitincount="0" availableitincount="0">
<success>
<priceditineraries><priceditinerary sequencenumber="1"></priceditinerary>
<priceditinerary sequencenumber="2"></priceditinerary></priceditineraries>
</success>
</ota_airlowfaresearchrs>
</soap-env:body>
</soap-env:envelope>
But when I tried with simplexml_load_string I am getting a problem to convert it to PHP array. what I tried is :
$parser = simplexml_load_string($res);
$parserEnv = $parser->children('soap-env', true);
$response = $parserEnv->body->ota_airlowfaresearchrs;
its return emptry array like : SimpleXMLElement Object ( )
From
$parser = simplexml_load_string($res);
you have to change like below:
$parser = simplexml_load_string($res, "SimpleXMLElement", LIBXML_NOCDATA);
LIBXML_NOCDATA : You can use this function call to have simplexml convert CDATA into plain text.
As ref to : converting SOAP XML response to a PHP object or array
I got the answer by doing the following way:
$soap = simplexml_load_string($res);
$response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')
->body->children()
->ota_airlowfaresearchrs
->success
->priceditineraries
thanks #lalithkumar any way
Related
How do I convert this XML to JSON
<?xml version="1.0" encoding="UTF-8"?>
<ns0:COMMAND xmlns:ns0="http://www.tibco.com/schemas/pinless/PINLESS.core/C2STransferBillPayment/Schema.xsd9">
<ns0:TYPE>EXRCTRFRESP</ns0:TYPE>
<ns0:TXNSTATUS>200</ns0:TXNSTATUS>
<ns0:DATE>02/02/20</ns0:DATE>
<ns0:EXTREFNUM>20200202192308729Af9cWtvg3W</ns0:EXTREFNUM>
<ns0:TXNID>R200202.1923.250008</ns0:TXNID>
<ns0:MESSAGE>R200202.1923.250008 confirmed. </ns0:MESSAGE>
</ns0:COMMAND>
I have tried doing this, but return empty array.
$string = '<?xml version="1.0" encoding="UTF-8"?><ns0:COMMAND xmlns:ns0="http://www.tibco.com/schemas/pinless/PINLESS.core/C2STransferBillPayment/Schema.xsd9"><ns0:TYPE>EXRCTRFRESP</ns0:TYPE><ns0:TXNSTATUS>200</ns0:TXNSTATUS><ns0:DATE>02/02/20</ns0:DATE><ns0:EXTREFNUM>20200202192308729Af9cWtvg3W</ns0:EXTREFNUM><ns0:TXNID>R200202.1923.250008</ns0:TXNID><ns0:MESSAGE>R200202.1923.250008 confirmed. </ns0:MESSAGE></ns0:COMMAND>';
$xmlObject = simplexml_load_string($string);
$jsonString = json_encode($xmlObject);
$jsonArray = json_decode($jsonString, true);
var_dump($jsonArray);
Anyone help?
First of all, please check out this PHP's built-in function simplexml_load_string in the doc to see what parameters does this function take and why? As your xml string is using namespace so you have to provide the namespace as the 4th argument. Look at the code below:
<?php
$xml_string = <<<EOR
<?xml version="1.0" encoding="UTF-8"?>
<ns0:COMMAND xmlns:ns0="http://www.tibco.com/schemas/pinless/PINLESS.core/C2STransferBillPayment/Schema.xsd9">
<ns0:TYPE>EXRCTRFRESP</ns0:TYPE>
<ns0:TXNSTATUS>200</ns0:TXNSTATUS>
<ns0:DATE>02/02/20</ns0:DATE>
<ns0:EXTREFNUM>20200202192308729Af9cWtvg3W</ns0:EXTREFNUM>
<ns0:TXNID>R200202.1923.250008</ns0:TXNID>
<ns0:MESSAGE>R200202.1923.250008 confirmed. </ns0:MESSAGE>
</ns0:COMMAND>
EOR;
$xml = simplexml_load_string($xml_string, "SimpleXMLElement", LIBXML_NOCDATA, 'ns0', true);
$json = json_encode($xml);
$array = json_decode($json, true);
echo '<pre>';
print_r($array);
// Outputs
Array
(
[TYPE] => EXRCTRFRESP
[TXNSTATUS] => 200
[DATE] => 02/02/20
[EXTREFNUM] => 20200202192308729Af9cWtvg3W
[TXNID] => R200202.1923.250008
[MESSAGE] => R200202.1923.250008 confirmed.
)
BTW you can do that using xml file too. In that case, you need to use this function simplexml_load_file() the same way.
<?php
$xml_file = 'your_xml_file_location.xml';
$xml = simplexml_load_file($xml_file, "SimpleXMLElement", LIBXML_NOCDATA, 'ns0', true);
this is _dorequest response. Response is xml string data.
I tried convert with simplexml_load_string() function and later return array but function result is empty.
'<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<env:Body>
<ns1:createNgiShipmentWithAddressResponse xmlns:ns1="http://yurticikargo.com.tr/NgiShipmentInterfaceServices" xmlns:ns2="http://yurticikargo.com.tr/NgiShipmentInterfaceServices" xmlns:ns3="http://yurticikargo.com.tr/WSExceptions/">
<XShipmentDataResponse>
<outFlag>0</outFlag>
<projectId>18501</projectId>
<specialFieldDataArray>
<specialFieldName>53</specialFieldName>
<specialFieldValue>SIP-1234885679</specialFieldValue>
</specialFieldDataArray>
<specialFieldDataArray>
<specialFieldName>3</specialFieldName>
<specialFieldValue>SIP-1234885679</specialFieldValue>
</specialFieldDataArray>
</XShipmentDataResponse>
</ns1:createNgiShipmentWithAddressResponse>
</env:Body>
</env:Envelope>'
xml converted to array:
$xml = "$result"; //$result xml string
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser,$xml,$vals,$index);
xml_parser_free($xml_parser);
$sonuc = $vals[$index['NODENAME'][0]]["value"];
I need to a value from this SOAP response. The value is in the loginresponse / return element. Here's the response:
<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" 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:body soap-env:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:DBCentralIntf-IDBCentral">
<ns1:loginresponse>
<return xsi:type="xsd:string"><**THIS IS THE VALUE I NEED**></return>
</ns1:loginresponse>
</soap-env:body>
Here's how I'm trying to parse:
$response = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($response, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['SOAP-ENV']);
$res = $soap->Body->children($ns['NS1']);
print_r($res->LoginResponse->Return);
But I get an empty object.
Thanks for your help!
Instead of using cURL and attempting to parse the XML response, consider using the PHP SOAP client. You may need to install PHP SOAP or enable it in your PHP configuration. (I'm using PHP on Windows, so I just had to uncomment extension=php_soap.dll in php.ini.)
If you have SOAP installed, you can get the WSDL from the provider of the web service you're using. Based on Googling this value in the XML you showed: xmlns:ns1="urn:DBCentralIntf-IDBCentral", I'm guessing you can find it here, but you'll probably have better luck finding it since you know for sure what web service you're using.
After you have the WSDL, using the PHP SOAP client is super easy:
$client = new SoapClient('path/to/your.wsdl');
$response = $client->Login(['username', 'password']);
$theValueYouNeed = $response->loginresponse->return;
UPDATE:
Removing the namespaces clears things up a bit (although a hack). Here my new code:
$response = curl_exec($ch);
curl_close($ch);
$cleanxml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $response);
$cleanxml = str_ireplace('NS1:','', $cleanxml);
$xml = simplexml_load_string($cleanxml);
echo $xml->Body->LoginResponse->return[0];
SOAP XML:
<?xml version="1.0" encoding="utf-8"?>
<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>
<PaymentNotification xmlns="http://apilistener.envoyservices.com">
<payment>
<uniqueReference>ESDEUR11039872</uniqueReference>
<epacsReference>74348dc0-cbf0-df11-b725-001ec9e61285</epacsReference>
<postingDate>2010-11-15T15:19:45</postingDate>
<bankCurrency>EUR</bankCurrency>
<bankAmount>1.00</bankAmount>
<appliedCurrency>EUR</appliedCurrency>
<appliedAmount>1.00</appliedAmount>
<countryCode>ES</countryCode>
<bankInformation>Sean Wood</bankInformation>
<merchantReference>ESDEUR11039872</merchantReference>
</payment>
</PaymentNotification>
</soap:Body>
</soap:Envelope>
How to get 'payment' element?
I try to parse (PHP)
$xml = simplexml_load_string($soap_response);
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
foreach ($xml->xpath('//payment') as $item)
{
print_r($item);
}
Result is empty :(
Any ideas how to parse it correct?
One of the simplest ways to handle namespace prefixes is simply to strip them from the XML response before passing it through to simplexml such as below:
$your_xml_response = '<Your XML here>';
$clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $your_xml_response);
$xml = simplexml_load_string($clean_xml);
This would return the following:
SimpleXMLElement Object
(
[Body] => SimpleXMLElement Object
(
[PaymentNotification] => SimpleXMLElement Object
(
[payment] => SimpleXMLElement Object
(
[uniqueReference] => ESDEUR11039872
[epacsReference] => 74348dc0-cbf0-df11-b725-001ec9e61285
[postingDate] => 2010-11-15T15:19:45
[bankCurrency] => EUR
[bankAmount] => 1.00
[appliedCurrency] => EUR
[appliedAmount] => 1.00
[countryCode] => ES
[bankInformation] => Sean Wood
[merchantReference] => ESDEUR11039872
)
)
)
)
PHP version > 5.0 has a nice SoapClient integrated. Which doesn't require to parse response xml. Here's a quick example
$client = new SoapClient("http://path.to/wsdl?WSDL");
$res = $client->SoapFunction(array('param1'=>'value','param2'=>'value'));
echo $res->PaymentNotification->payment;
In your code you are querying for the payment element in default namespace, but in the XML response it is declared as in http://apilistener.envoyservices.com namespace.
So, you are missing a namespace declaration:
$xml->registerXPathNamespace('envoy', 'http://apilistener.envoyservices.com');
Now you can use the envoy namespace prefix in your xpath query:
xpath('//envoy:payment')
The full code would be:
$xml = simplexml_load_string($soap_response);
$xml->registerXPathNamespace('envoy', 'http://apilistener.envoyservices.com');
foreach ($xml->xpath('//envoy:payment') as $item)
{
print_r($item);
}
Note: I removed the soap namespace declaration as you do not seem to be using it (it is only useful if you would use the namespace prefix in you xpath queries).
$xml = '<?xml version="1.0" encoding="utf-8"?>
<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>
<PaymentNotification xmlns="http://apilistener.envoyservices.com">
<payment>
<uniqueReference>ESDEUR11039872</uniqueReference>
<epacsReference>74348dc0-cbf0-df11-b725-001ec9e61285</epacsReference>
<postingDate>2010-11-15T15:19:45</postingDate>
<bankCurrency>EUR</bankCurrency>
<bankAmount>1.00</bankAmount>
<appliedCurrency>EUR</appliedCurrency>
<appliedAmount>1.00</appliedAmount>
<countryCode>ES</countryCode>
<bankInformation>Sean Wood</bankInformation>
<merchantReference>ESDEUR11039872</merchantReference>
</payment>
</PaymentNotification>
</soap:Body>
</soap:Envelope>';
$doc = new DOMDocument();
$doc->loadXML($xml);
echo $doc->getElementsByTagName('postingDate')->item(0)->nodeValue;
die;
Result is:
2010-11-15T15:19:45
First, we need to filter the XML so as to parse that into an object
$response = strtr($xml_string, ['</soap:' => '</', '<soap:' => '<']);
$output = json_decode(json_encode(simplexml_load_string($response)));
var_dump($output->Body->PaymentNotification->payment);
This is also quite nice if you subsequently need to resolve any objects into arrays:
$array = json_decode(json_encode($responseXmlObject), true);
First, we need to filter the XML so as to parse that change objects become array
//catch xml
$xmlElement = file_get_contents ('php://input');
//change become array
$Data = (array)simplexml_load_string($xmlElement);
//and see
print_r($Data);
why don't u try using an absolute xPath
//soap:Envelope[1]/soap:Body[1]/PaymentNotification[1]/payment
or since u know that it is a payment and payment doesn't have any attributes just select directly from payment
//soap:Envelope[1]/soap:Body[1]/PaymentNotification[1]/payment/*
I have response from webservice:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<ns1:H1 xsi:type="ns1:H1">
<BOGUS>
<time>1411967345</time>
<status>1</status>
<speed>0</speed>
</BOGUS>
<BOGUS>
<time>1411964888</time>
<status>10</status>
<speed>0</speed>
</BOGUS>
</ns1:H1>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
How can I access to element time or status in BOGUS[0] or BOGUS[1]?
I tried this:
$soap = simplexml_load_string($str);
$response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://tempuri.org/')->H1;
$time = $response->BOGUS[1]->time;
echo $time;
, but it's not working. Returns: Notice: Trying to get property of non-object
tempuri.org is right. I pasted xml response on: xmlgrid.net and got correct tree.
I'd recommandate to use Zend Soap Client for PHP. There u can do like this:
$client = new Zend_Soap_Client("MyService.wsdl");
$result = $client->yourMethod(<YouParameters ...>);
echo $result->H1->BOGUS[1]->time;
See:
http://framework.zend.com/manual/1.12/de/zend.soap.client.html
You can do it by loops as you are getting array in return
foreach ($response as $res)
{
$time = $res->BOGUS[1]->time;
echo $time;
}