【问题标题】:How do i display a news feed from an external page with a PHP array?如何使用 PHP 数组显示来自外部页面的新闻提要?
【发布时间】:2014-05-23 03:15:18
【问题描述】:

在这个网站:(http://feeds.feedburner.com/aflcomau),有很多我想获得的元素在我自己的网站上显示。我想要获取的元素是每条新闻的标题、发布时间、描述以及每条新闻中的图片。

在他们的网站上他们有:

<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>

有谁知道如何将它放入一个数组中,其中有许多包含HeadlineTimePostedDescriptionImage 数组的新闻项目?

【问题讨论】:

  • 这些网站有 RSS 源吗?
  • @Machavity:是的,他们确实有 RSS 提要。网站上有一个名为View Feed XML的链接。

标签: php arrays rss feed


【解决方案1】:

首先,您需要使用file_get_contents() 来获取提要。之后就可以使用simplexml_load_string()解析数据并获取。考虑这个例子:

$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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 2013-02-07
    • 2011-09-29
    相关资源
    最近更新 更多