How to read xml from url in php?

you can get the data from the XML by using "simplexml_load_file" Function. Please refer this link

http://php.net/manual/en/function.simplexml-load-file.php

$url = "http://maps.google.com/maps/api/directions/xml?origin=Quentin+Road+Brooklyn%2C+New+York%2C+11234+United+States&destination=550+Madison+Avenue+New+York%2C+New+York%2C+10001+United+States&sensor=false";
$xml = simplexml_load_file($url);
print_r($xml);

answered Sep 22, 2012 at 9:17

2

Your code seems right, check if you have fopen wrappers enabled (allow_url_fopen = On on php.ini)

Also, as mentioned by other answers, you should provide a properly encoded URI or encode it using urlencode() function. You should also check if there is any error fetching the XML string and if there is any parsing error, which you can output using libxml_get_errors() as follows:

<?php
if (($response_xml_data = file_get_contents($map_url))===false){
    echo "Error fetching XML\n";
} else {
   libxml_use_internal_errors(true);
   $data = simplexml_load_string($response_xml_data);
   if (!$data) {
       echo "Error loading XML\n";
       foreach(libxml_get_errors() as $error) {
           echo "\t", $error->message;
       }
   } else {
      print_r($data);
   }
}
?>

If the problem is you can't fetch the XML code maybe it's because you need to include some custom headers in your request, check how to use stream_context_create() to create a custom stream context for use when calling file_get_contents() on example 4 at http://php.net/manual/en/function.file-get-contents.php

answered Sep 22, 2012 at 9:25

NotGaeLNotGaeL

8,1945 gold badges38 silver badges69 bronze badges

3

file_get_contents() usually has permission issues. To avoid them, use:

function get_xml_from_url($url){
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

    $xmlstr = curl_exec($ch);
    curl_close($ch);

    return $xmlstr;
}

Example:

$xmlstr = get_xml_from_url('http://www.camara.gov.br/SitCamaraWS/Deputados.asmx/ObterDeputados');
$xmlobj = new SimpleXMLElement($xmlstr);
$xmlobj = (array)$xmlobj;//optional

Rotimi

4,7054 gold badges17 silver badges27 bronze badges

answered Apr 19, 2016 at 2:01

How to read xml from url in php?

DoglasDoglas

5629 silver badges22 bronze badges

0

$url = 'http://www.example.com'; $xml = simpleXML_load_file($url,"SimpleXMLElement",LIBXML_NOCDATA);

$url can be php file, as long as the file generate xml format data as output.

answered Oct 12, 2017 at 20:28

How to read xml from url in php?

li bing zhaoli bing zhao

1,33012 silver badges12 bronze badges

It is working for me. I think you probably need to use urlencode() on each of the components of $map_url.

answered Sep 22, 2012 at 9:17

Brett ZamirBrett Zamir

13.5k6 gold badges51 silver badges74 bronze badges

simplexml_load_file interprets an XML file into an object. This tutorial describes how to parse an XML document or RSS feed that follows a known schema.

We can directly iterate over SimpleXML’s elements / items using foreach.

Read RSS Feed from a URL

$rss = simplexml_load_file($url);

foreach ($rss->channel as $channel){
 foreach ($channel->item as $item){
  echo $item->link.'<br>';
  echo $item->title.'<br>';
  echo $item->description.'<br>';
 }
}

Parsing an XML file

SimpleXML is also useful to read a configuration file written in XML or process the result of a REST request.

Here’s an example of book store in XML.

$books = simplexml_load_file('bookstore.xml');
foreach ($books as $book) {
 echo $book->name.'<br>';
 echo $book->price.' $<br>';
 echo $book->date.'<br>';
 echo '<b>Authors:</b><br>';
 foreach ($book->author as $author){
  echo $author->firstName.'<br>';
  echo $author->lastName.'<br>';
 }
 echo '<hr>';
}

Sample bookstore.xml file to parse.

<?xml version="1.0"?>
<books>
 <book>
  <name>PHP Book</name>
  <date>20016-09-24</date>
  <price>5.00</price>
  <author>
   <firstName>FName</firstName>
   <lastName>LName</lastName>
  </author>
  <author>
   <firstName>FName2</firstName>
   <lastName>LName2</lastName>
  </author>
 </book>
 <book>
  <name>XML Book</name>
  <date>20016-12-24</date>
  <price>10.00</price>
  <author>
   <firstName>First Name</firstName>
   <lastName>Last Name</lastName>
  </author>
 </book>
</books>

by updated Aug 26, 2016

How to read XML file from url PHP?

$url = 'http://www.example.com'; $xml = simpleXML_load_file($url,"SimpleXMLElement",LIBXML_NOCDATA); $url can be php file, as long as the file generate xml format data as output. Show activity on this post. It is working for me.

What is PHP XML?

PHP XML Parser Introduction XML is a data format for standardized structured document exchange. More information on XML can be found in our XML Tutorial. This extension uses the Expat XML parser. Expat is an event-based parser, it views an XML document as a series of events.

What is XML used for?

What is XML? The Extensible Markup Language (XML) is a simple text-based format for representing structured information: documents, data, configuration, books, transactions, invoices, and much more. It was derived from an older standard format called SGML (ISO 8879), in order to be more suitable for Web use.