Tags
Asked 3 years ago
30 Jun 2021
Views 285
Weldon

Weldon posted

how to read xml file from url using php

how to read xml file from url using php
jignesh

jignesh
answered Apr 28 '23 00:00

You can read an XML file from a URL using PHP by using the simplexml_load_file () function. Here is an example:



$url = 'https://example.com/data.xml';
$xml = simplexml_load_file($url);
In the example above, the URL of the XML file is stored in the $url variable. The simplexml_load_file() function is then used to load the contents of the XML file into the $xml variable.


Once the XML file has been loaded, you can access its contents using the SimpleXML library. For example, if the XML file contains a list of products, you could loop through the products like this:


foreach ($xml->product as $product) {
    echo $product->name . ': ' . $product->price . '<br>';
}

In this example, the foreach loop is used to iterate through each <product> element in the XML file. The name and price of each product are then displayed on the page.
Post Answer