dongzongxun8491 2014-05-23 03:15
浏览 23
已采纳

如何使用PHP数组从外部页面显示新闻源?

In this website: (http://feeds.feedburner.com/aflcomau), there many elements in which I would like to obtain to display on my own website. The elements on which I want to obtain are the Title of each news item, their posted time, the description of it and along with the picture in each news.

On their website they have:

<h3 id="currentFeedContent">Current Feed Content</h3>
<ul>
<li class="regularitem" xmlns:dc="http://purl.org/dc/elements/1.1/">
<h4 class="itemtitle">
<a href="http://feedproxy.google.com/~r/aflcomau/~3/mR0Dt1yzINw/are-you-old-enough">Are you old enough? </a>
</h4>
<h5 class="itemposttime">
<span>Posted:</span>Thu, 22 May 2014 23:54:24 GMT</h5>
<div class="itemcontent" name="decodeable"><img width="60" style="float:right;" src="http://www.afl.com.au/staticfile/AFL Tenant/Media/Images/292275-tlssmallthumbnail.jpg" alt="2013 NAB AFL U18 Championship - Vic Metro v SA">
<p>Four gun draftees have different takes on the best age to join AFL ranks</p><img src="http://feeds.feedburner.com/~r/aflcomau/~4/mR0Dt1yzINw" height="1" width="1" style="display: none !important;"></div>
</li>
<li class="regularitem">
<h4 class="itemtitle">
<a href="http://feedproxy.google.com/~r/aflcomau/~3/6O720TT5QWA/tigers-back-hardwick-">Tigers back Hardwick </a>
</h4>
<h5 class="itemposttime">
<span>Posted:</span>Thu, 22 May 2014 22:41:05 GMT</h5>
<div class="itemcontent" name="decodeable"><img width="60" style="float:right;" src="http://www.afl.com.au/staticfile/AFL Tenant/Media/Images/325961-tlssmallthumbnail.jpg" alt="AFL 2014 Rd 07 - Geelong v Richmond">
<p>Tigers president breaks silence to back her embattled coach</p><img src="http://feeds.feedburner.com/~r/aflcomau/~4/6O720TT5QWA" height="1" width="1" style="display: none !important;"></div>
</li>
</ul>

Does any one know how to put it into an array where there are many news items that contains arrays of Headline, TimePosted, Description and Image?

  • 写回答

1条回答 默认 最新

  • dqwh1202 2014-05-23 03:52
    关注

    First off, you need to use file_get_contents() to get the feed. Then after that, you can use simplexml_load_string() to parse the data and get it. Consider this example:

    $data = array();
    $contents = file_get_contents('http://feeds.feedburner.com/aflcomau?format=xml');
    $xml = simplexml_load_string($contents);
    $x = 0;
    foreach($xml->channel->item as $key => $value) {
        foreach($value as $index => $element) {
            if($index == 'description') {
                // get the proper description
                $description = $element;
                preg_match('/src=(["\'])(.*?)\1/', $description, $match);
                $data[$x]['image_link'] = $match[2];
                preg_match('#<p[^>]*>(.*?)</p>#', $description, $match2);
                $data[$x]['description'] = $match2[1];
            } else {
                $data[$x][$index] = htmlentities($element);
            }
        }
        $x++;
    }
    
    print_r($data);
    

    Sample Fiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装