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

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀