How to retrieve XML interface data using PHP?

To read XML interface data, you can utilize the SimpleXML extension in PHP.

Below is a simple example demonstrating how to read XML interface data.

<?php
$url = "http://example.com/api/data.xml"; // XML接口的URL

// 读取XML数据
$xml = simplexml_load_file($url);

// 遍历XML数据
foreach ($xml->children() as $item) {
    $name = $item->name;
    $age = $item->age;
    $gender = $item->gender;

    // 在这里可以对数据进行处理或输出
    echo "Name: " . $name . "<br>";
    echo "Age: " . $age . "<br>";
    echo "Gender: " . $gender . "<br><br>";
}
?>

In the example above, we first specify a URL for an XML interface, then use the simplexml_load_file() function to read the XML data and convert it into a SimpleXMLElement object. Next, we can use a foreach loop to iterate through the XML data and access the values of XML elements using object properties or methods. In this case, we accessed the values of the , , and child elements of each element and outputted them to the page.

Please note that in practical applications, you may need to make corresponding adjustments based on the structure of the XML data.

广告
Closing in 10 seconds
bannerAds