dongzhang4301 2015-12-20 09:22
浏览 37
已采纳

如何使用PHP读取XML API

I am learning PHP and need to know how to get the elements in following XML API into an Array. I tried with simplexml_load_file() function. But it gave me an error.

Here are my code.

<?php
$xml=simplexml_load_file("http://static.cricinfo.com/rss/livescores.xml");

foreach($xml->getElementsByTagName('item') as $match)
{
    echo $match->getAttribute('title'), "
";
}

?>
  • 写回答

2条回答 默认 最新

  • douxueke5653 2015-12-20 09:52
    关注

    Please use the following code for it.

    $xml=simplexml_load_file("http://static.cricinfo.com/rss/livescores.xml");
    $channel_info = $xml->channel;
    $totalRecords = count($channel_info->item);
    for ($i=0; $i < $totalRecords; $i++) { 
        echo $channel_info->item[$i]->title . '<br/>';
    }
    

    I hope it will work for you.

    Thank you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?