blobs
blobs

Reputation: 86

How do i display a news feed from an external page with a PHP array?

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?

Upvotes: 0

Views: 247

Answers (1)

user1978142
user1978142

Reputation: 7948

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

Upvotes: 2

Related Questions