Unable to convert OTA API xml response? - php

I'm trying to parse OTA service's XML response in PHP. I have tried below code but no luck.
$doc = new DOMDocument();
$doc->loadXML($response);
$XMLresults = $doc->getElementsByTagName("OTA_VehAvailRateRS");
I want to get different tags value from xml response. Can someone help me out from this? Thanks. For example i want to parse following xml response:
<?xml version="1.0"?>
<OTA_VehAvailRateRS xmlns="http://www.opentravel.org/OTA/2003/05" TimeStamp="2018-02-28T01:12:27" Target="Test" Version="4.500" SequenceNmbr="1">
<Success/>
<VehAvailRSCore>
<VehRentalCore PickUpDateTime="2018-03-13T08:00:00" ReturnDateTime="2018-03-30T08:00:00">
<PickUpLocation LocationCode="TEST"/>
<ReturnLocation LocationCode="TEST"/>
</VehRentalCore>
<VehVendorAvails>
<VehVendorAvail>
<VehAvails> <VehAvail>
<VehAvailCore Status="Available">
<Vehicle Code="TEST15" VendorCarType="TEST15" Description="TEST"/>
<RentalRate>
<RateDistance Unlimited="True" DistUnitName="Mile" VehiclePeriodUnitName="RentalPeriod"/>
<VehicleCharges>
<VehicleCharge Description="TEST" Amount="1299.35" CurrencyCode="" GuaranteedInd="True" Purpose="1"> <Calculation UnitCharge="33.32" UnitName="Hour"/>
<Calculation UnitCharge="1560.00" UnitName="Month"/>
<Calculation UnitCharge="499.75" UnitName="Week"/>
<Calculation UnitCharge="99.95" UnitName="Day"/>
<TaxAmounts> </TaxAmounts>
</VehicleCharge>
</VehicleCharges>
</RentalRate>
<Fees> </Fees>
<TotalCharge CurrencyCode="" RateTotalAmount="1299.35" EstimatedTotalAmount="1299.35"/>
<PricedEquips> </PricedEquips> </VehAvailCore> <VehAvailInfo> <PricedCoverages> </PricedCoverages> </VehAvailInfo> </VehAvail> </VehAvails> </VehVendorAvail> </VehVendorAvails> </VehAvailRSCore> </OTA_VehAvailRateRS>

I have found solution. I have used simplexml_load_string() function for parse response. For ex:
$xml = simplexml_load_string($finalresponse);
var_dump($xml); //getting parsed response here
For getting value of different xml nodes i have used below code:
$nodevalue = $xml->VehAvailRSCore->VehRentalCore->attributes()->PickUpDateTime; //this code gives me "2018-03-13T08:00:00" as result

Related

How to move XML file data to PHP array?

This is my xml data
<?xml version="1.0" encoding="iso-8859-1"?>
<smslist>
<sms>
<cid>FIRSTCID</cid>
<mid>FIRSTMID</mid>
<mb>98389923</mb>
</sms>
<sms>
<cid>SECONDCID</cid>
<mid>SECONDMID</mid>
<mb>76445645</mb>
</sms>
...
</smslist>
How to push cid and mid data to php array like $array = array(("FIRSTCID","FIRSTMID"),("SECONDCID","SECONDMID")...)
Excuse if this is some duplicate question. :)
you can try this:
$xml = new SimpleXMLElement($your_xml_string);
$xml_array=[];
foreach ($xml->smslist->sms as $sms) {
$xml_array[]=array($sms->cid,$sms->mid);
}
By using xml_parse_into_struct() you can convert XML to array, for detailed document check below link.
http://php.net/manual/en/function.xml-parse-into-struct.php

get xml elemnts data with php

I get xml object using curl.
But I can not seem to enter xml elements data.
I tried using:
$sxe = new SimpleXMLElement($result);
$final_xml = $sxe->asXML();
echo $final_xml -> Answer -> Status;
With that example I got nothing.
OR
$xml = simplexml_load_string($sxe);
$json = json_encode($xml);
$array = json_decode($json);
With that example I got an array of the xml as a long string and each position in the array was different note in the xml string.
This is the xml output I get, that I want to retrieve data from:
how can I retrieve data from this xml or change it to JSON and then retrieve data?
Thanx
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns7:GetStopMonitoringServiceResponse xmlns:ns3="http://www.siri.org.uk/siri" xmlns:ns4="http://www.ifopt.org.uk/acsb" xmlns:ns5="http://www.ifopt.org.uk/ifopt" xmlns:ns6="http://datex2.eu/schema/1_0/1_0" xmlns:ns7="http://new.webservice.namespace">
<Answer>
<ns3:ResponseTimestamp>2014-12-04T11:11:55.585+02:00</ns3:ResponseTimestamp>
<ns3:ProducerRef>ISR Siri Server (141.10)</ns3:ProducerRef>
<ns3:ResponseMessageIdentifier>89061165</ns3:ResponseMessageIdentifier>
<ns3:RequestMessageRef>000234:1351677777:4684</ns3:RequestMessageRef>
<ns3:Status>true</ns3:Status>
<ns3:StopMonitoringDelivery version="IL2.7">
<ns3:ResponseTimestamp>2014-12-04T11:11:55.585+02:00</ns3:ResponseTimestamp>
<ns3:RequestMessageRef>0</ns3:RequestMessageRef>
<ns3:Status>true</ns3:Status>
<ns3:MonitoredStopVisit>
<ns3:RecordedAtTime>2014-12-04T11:11:00.000+02:00</ns3:RecordedAtTime>
<ns3:ItemIdentifier>1448001046</ns3:ItemIdentifier>
<ns3:MonitoringRef>40262</ns3:MonitoringRef>
<ns3:MonitoredVehicleJourney>
<ns3:LineRef>4687</ns3:LineRef>
<ns3:DirectionRef>3</ns3:DirectionRef>
<ns3:PublishedLineName>12</ns3:PublishedLineName>
<ns3:OperatorRef>3</ns3:OperatorRef>
<ns3:DestinationRef>40110</ns3:DestinationRef>
<ns3:OriginAimedDepartureTime>2014-12-04T10:45:00.000+02:00</ns3:OriginAimedDepartureTime>
<ns3:VehicleLocation>
<ns3:Longitude>34.94065475463867</ns3:Longitude>
<ns3:Latitude>32.428466796875</ns3:Latitude>
</ns3:VehicleLocation>
<ns3:MonitoredCall>
<ns3:StopPointRef>40262</ns3:StopPointRef>
<ns3:ExpectedArrivalTime>2014-12-04T11:12:00.000+02:00</ns3:ExpectedArrivalTime>
</ns3:MonitoredCall>
</ns3:MonitoredVehicleJourney>
</ns3:MonitoredStopVisit>
</ns3:StopMonitoringDelivery>
</Answer>
</ns7:GetStopMonitoringServiceResponse>
</S:Body>
</S:Envelope>
Try this test code:
<pre><?php
$entries = simplexml_load_file("test.xml");
$namespaces = $entries->getNamespaces(true);
var_dump($namespaces);
$ns3s = $entries->children($namespaces['S'])
->Body
->children($namespaces['ns7'])
->GetStopMonitoringServiceResponse
->children()
->Answer
->children($namespaces['ns3']);
var_dump($ns3s);

Read Array Object from WSDL XML with namespaces in PHP

I am sending array of objects in XML Soap response from my java code to php in my project using WSDL.
I want to store values in php for my project.
I tried many ways but couldn't able to find how to parse my xml and read values. I am not export in xml area.
Please anyone help me for read my values from values.
My SOAP Response Body:
<soapenv:envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" \xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:body>
<getactualtimerequestresponse xmlns="http://impl.sample.com">
<getactualtimereturn>.
<ns1:projectlist xmlns:ns1="http://response.sample.com">
<item>
<ns2:userid xmlns:ns2="http://request.sample.com">4</ns2:userid>
<ns3:username xmlns:ns3="http://request.sample.com">Manoj Arun</ns3:username>
</item>
<item>
<ns5:userid xmlns:ns5="http://request.sample.com">5</ns5:userid>
<ns6:username xmlns:ns6="http://request.sample.com">Sethu Raman</ns6:username>
</item>
</ns1:projectlist>
<ns10:message xsi:nil="true" xmlns:ns10="http://response.sample.com"></ns10:message>
</getactualtimereturn>
</getactualtimerequestresponse>
</soapenv:body>
</soapenv:envelope>
projectList is my object created in java.
In PHP:
I tried to read like below but i didn't got anything.
foreach($xml->xpath('//ns:projectList') as $header)
{
foreach($header->item as $userIds)
{
echo $userIds->xpath('//ns:userId');
}
}
Thanks in advance...
$client = new SoapClient('http://url.com?wsdl');
$params = array(Java arg => php value);
$result = $client->Function( $parms );
print_r($result);

How do I parse an individual entry from XML using PHP?

I am trying to parse an individual element from an XML string using PHP. The issue is that this individual element occurs before the entries start. The XML is below:
<?xml version="1.0" encoding="UTF-8"?>
<feed gd:kind="shopping#products" gd:etag=""lm_25heFT8yiumci9EH1kItJBpg/Sj5O9aXZ82PKpx3N2C3uQYMhNYE"" xmlns="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:s="http://www.google.com/shopping/api/schemas/2010">
<openSearch:totalResults>64</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry >...</entry>
<entry >...</entry>
</feed>
I am trying to parse out the "64" in the opensearch:totalResults tag. How do I this and assign it to a variable in php? I tried:
$url = 'url of xml feed';
$xml = simplexml_load_file($url);
$entries =$xml->entry[0]->openSearch:totalResults;
// also tried $entries =$xml->openSearch:totalResults;
echo $entries;
but it's not working. Any advice?
You need to register namespace in order to access these nodes:
$xml = simplexml_load_file('file.xml');
$xml->registerXPathNamespace('os', 'http://a9.com/-/spec/opensearchrss/1.0/');
$nodes = $xml->xpath('os:totalResults');
$totalResults = (string)$nodes[0];
You can also use http://it1.php.net/manual/en/simplexmlelement.children.php (using the $ns parameter)
that is less resource intensive.

hostip.info - Parse API response with SimpeXML

I'm trying to parse the xml response of http://api.hostip.info/?ip=12.215.42.19 with SimpleXML but I can't seem to get it work.
Response
<?xml version="1.0" encoding="ISO-8859-1" ?>
<HostipLookupResultSet version="1.0.1" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.hostip.info/api/hostip-1.0.1.xsd">
<gml:description>This is the Hostip Lookup Service</gml:description>
<gml:name>hostip</gml:name>
<gml:boundedBy>
<gml:Null>inapplicable</gml:Null>
</gml:boundedBy>
<gml:featureMember>
<Hostip>
<ip>12.215.42.19</ip>
<gml:name>Sugar Grove, IL</gml:name>
<countryName>UNITED STATES</countryName>
<countryAbbrev>US</countryAbbrev>
<!-- Co-ordinates are available as lng,lat -->
<ipLocation>
<gml:pointProperty>
<gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">
<gml:coordinates>-88.4588,41.7696</gml:coordinates>
</gml:Point>
</gml:pointProperty>
</ipLocation>
</Hostip>
</gml:featureMember>
</HostipLookupResultSet>
Can someone help me to access for instance Hostip>ipLocation>pointProperty>Point>coordinates
Thanks in advance!
Here are two ways (which are available elsewhere on SO by searching!).
XPath (a very basic on to demonstrate)
$coords = $xml->xpath('//gml:coordinates');
echo $coords[0];
"Simple" XML (not so simple)
echo $xml
->children('gml', TRUE)->featureMember
->children('', TRUE)->Hostip->ipLocation
->children('gml', TRUE)->pointProperty->Point->coordinates;
You can access attributes like array
http://www.electrictoolbox.com/php-simplexml-element-attributes/
like (im not sure with your example)
Hostip->ipLocation->gml['pointproperty']

Categories