Read xml response using php

Nirali Joshi

I have a Api response in XML format.How can I get gps_x and gps_y for both elements.

$url="http://www.tixik.com/api/nearby?lat=36.106121163930377&lng=28.07762145996093&limit=2&key=demo";

$xmlinfo = simplexml_load_file($url);

print_r($xmlinfo);

echo $xmlinfo['gps_x']; // outputs nothing
echo $xmlinfo -> gps_x; // outputs nothing

How can I get gps_x and gps_y from above response?

Rohan Khude

I did this by getting the content from url then converting to json using exceptions handling and get the data from decoded json:

<?php   

$myXMLData = file_get_contents("http://www.tixik.com/api/nearby?lat=36.106121163930377&lng=28.07762145996093&limit=2&key=demo");
$simpleXml = simplexml_load_string($myXMLData) or die("Error: Cannot create encode data to xml object");
$jsondata = json_encode($simpleXml) or die("Error: Cannot encode record to json");
$data = json_decode($jsondata, true);
$in = $data['items']['item'];
foreach ($in as $key => $value) {
    echo "ID= " . $in[$key]['id'] . ", GPS-x = " . $in[$key]['gps_x'] . ", GPS-y = " . $in[$key]['gps_x'];
    echo "<br/>";
}   

?>

OUTPUT

ID= 2354292, GPS-x = 36.1065000000, GPS-y = 36.1065000000

ID= 2431066, GPS-x = 36.0949905151, GPS-y = 36.0949905151

If you want to take the data from XML directly:

<?php   

$myXMLData = file_get_contents("http://www.tixik.com/api/nearby?lat=36.106121163930377&lng=28.07762145996093&limit=2&key=demo");
$simpleXml = simplexml_load_string($myXMLData) or die("Error: Cannot create encode data to xml object");
$in = $simpleXml->items->item;
foreach ($in as $key) {
    echo "ID= " . $key->id;
    echo ", GPS-x = " . $key->gps_x;
    echo ", GPS-y = " . $key->gps_y . "<br/>";
}

?>

OUTPUT

ID= 2354292, GPS-x = 36.1065000000, GPS-y = 28.0684000000

ID= 2431066, GPS-x = 36.0949905151, GPS-y = 28.0860328674

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to read soap response xml in php

From Dev

How to read soap response xml in php

From Dev

How to Read XML Response using Java

From Dev

How to force XML response using PHP CURL

From Dev

Read xml values in namespaces/SOAP response using groovy XMLParser

From Dev

Unable to read XML tags with '-' using SimpleXMLElement in PHP

From Dev

Cannot Read GCM server response using JAXL php library

From Dev

Read XML Response From Page

From Dev

Read xml response in unity Android

From Dev

convert XML to JSON using PHP/JQuery- response should be in JSON

From Dev

how read the data from XML file using php

From Dev

how read the data from XML file using php

From Dev

Problems to read a XML with PHP

From Dev

How to read 2nd element of list of xml items returned in response using ExtractVariables policy in apigee?

From Dev

How to read 2nd element of list of xml items returned in response using ExtractVariables policy in apigee?

From Dev

Jmeter code to read response xml attributes in beanshell post processor without using Xpath Extractor?

From Dev

parsing SOAP XML response with php

From Dev

Nest XML soap response PHP

From Dev

Read xml using boost

From Dev

Read XML using SelectSingleNode

From Dev

using xmldocument to read xml

From Dev

Read XML using XStream

From Dev

Read xml using boost

From Dev

How to use HttpClient to read an XML response?

From Dev

Deserializing XML Response using RestSharp

From Dev

Parse xml response using retrofit

From Dev

How to read JSON response while using $.post to send & receive data to PHP alongwith done & fail functions

From Dev

How to read JSON response while using $.post to send & receive data to PHP alongwith done & fail functions

From Dev

Read XML PHP Internal and External

Related Related

  1. 1

    How to read soap response xml in php

  2. 2

    How to read soap response xml in php

  3. 3

    How to Read XML Response using Java

  4. 4

    How to force XML response using PHP CURL

  5. 5

    Read xml values in namespaces/SOAP response using groovy XMLParser

  6. 6

    Unable to read XML tags with '-' using SimpleXMLElement in PHP

  7. 7

    Cannot Read GCM server response using JAXL php library

  8. 8

    Read XML Response From Page

  9. 9

    Read xml response in unity Android

  10. 10

    convert XML to JSON using PHP/JQuery- response should be in JSON

  11. 11

    how read the data from XML file using php

  12. 12

    how read the data from XML file using php

  13. 13

    Problems to read a XML with PHP

  14. 14

    How to read 2nd element of list of xml items returned in response using ExtractVariables policy in apigee?

  15. 15

    How to read 2nd element of list of xml items returned in response using ExtractVariables policy in apigee?

  16. 16

    Jmeter code to read response xml attributes in beanshell post processor without using Xpath Extractor?

  17. 17

    parsing SOAP XML response with php

  18. 18

    Nest XML soap response PHP

  19. 19

    Read xml using boost

  20. 20

    Read XML using SelectSingleNode

  21. 21

    using xmldocument to read xml

  22. 22

    Read XML using XStream

  23. 23

    Read xml using boost

  24. 24

    How to use HttpClient to read an XML response?

  25. 25

    Deserializing XML Response using RestSharp

  26. 26

    Parse xml response using retrofit

  27. 27

    How to read JSON response while using $.post to send & receive data to PHP alongwith done & fail functions

  28. 28

    How to read JSON response while using $.post to send & receive data to PHP alongwith done & fail functions

  29. 29

    Read XML PHP Internal and External

HotTag

Archive