简体   繁体   中英

Trying to read values from an XML feed and display them in PHP

I'm trying to display values from this xml feed: http://www.scorespro.com/rss/live-soccer.xml

In my PHP code I have the following loop but it does not display the results on my page:

<?php
    $xml = simplexml_load_file("http://www.scorespro.com/rss/live-soccer.xml");

    echo $xml->getName() . "<br>";

    foreach($xml->children() as $item)
    {
        echo $item->getName() . ": " . $item->name . "<br>";
    }
?>

For some reason it only shows:

rss
channel: 

I'm fairly new to how XML works so any help would be much appreciated.

You can get actual data from $xml->channel->item so use like below

$items = $xml->channel->item;    
foreach($items as $item) {
  $title = $item->title;
  $link = $item->link;
  $pubDate = $item->pubDate;
  $description = $item->description;
}

DEMO.

you can use below code

$dom = new DOMDocument;
$dom->loadXML($url);
if (!$dom) {
    echo 'Error while parsing the document';
    exit;
}
$xml = simplexml_import_dom($dom);
$data = $xml->channel->item;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM