Ask your WordPress questions! Pay money and get answers fast! (more info)

XML/RSS data feeds integration WordPress

  • SOLVED

I need help to pull an XML / RSS feed in a WordPress page.

This is how the feed will need to show in the end (the HTML/CSS is written already)
[[LINK href="http://cl.ly/0A0X260C0u0B2y0I1f2y"]]http://cl.ly/0A0X260C0u0B2y0I1f2y[[/LINK]]

Here is a sample of the feed provided by Ticketsoft:


<advShowtimesRow>
<TheatreID>1008</TheatreID>
<title>The Avengers - PG13 - 140 mins</title>
<link>
https://domain.aspx?theatreid=1008&ShowDate=05/31/2012
</link>
<description>05/31/2012 3:30PM</description>
<pubDate>05/31/2012</pubDate>
<BusinessDate>05/31/2012</BusinessDate>
<Enclosure>
http://domain.com/images/MoviePosters/4918.jpg
</Enclosure>
<TSMovieID>4918</TSMovieID>
</advShowtimesRow>


The date dropdown would need to sort the feeds by date, and there four locations with separate id's so we need to pull the feed based on location id.

Does anyone have experience or can help? Do we need a plugin or function?

Answers (2)

2012-05-31

John Cotton answers:

If I were you, I'd write my own function - plugins rarely do precisely what you want.

You can use something like this to get data:

if( $xml = simplexml_load_file($request_url) ) {
// Do something with $xml
// e.g. if <shows> is a list of those available then
foreach( $xml->shows as $show ) {
// output the info
}
}


If the XML is neatly laid out, you should be able to use [[LINK href="http://uk3.php.net/manual/en/function.asort.php"]]asort[[/LINK]] to sort the array as you want. After that, you can just output the data into your HTML.


Lucian Florian comments:

Hey John, what would be the suggested prize to help with coding that solution?


John Cotton comments:

Well, you can hire me for $100 an hour, but let's see if we can't get you there before that's necessary ;)

1/ Do you have a single url to get the XML back?
2/ If you do, stick it in the code above, add a print_r($xml) within the if statement and send me a link to a page with the output.


Lucian Florian comments:

sent a PM


John Cotton comments:

I'm not quite sure what your output is supposed to be, but this should give you enough to tweak to what you actually want.....


function sort_by_date($a, $b) {
$a_time = strtotime($a->BusinessDate);
$b_time = strtotime($b->BusinessDate);

if ($a_time == $b_time) {
return 0;
}
return ($a_time < $b_time) ? -1 : 1;
}


if( $xml = simplexml_load_file($request_url) ) {
usort($xml->AdvShowtimesDetailRow, 'sort_by_date'); // sorts the main array by date ASC

$shows = array(); // Empty array to hold our HTML

foreach( $xml->AdvShowtimesDetailRow as $show ) {
$shows[] = sprintf( '<option value="%d">%s</option>', $show->TSMovieID, $show->title );
}

echo '<select id="shows">'.implode( '', $shows ).</select>'; // join the option elements and output
}


Lucian Florian comments:

Hey John, thanks. Your solution is good and I have somebody helping me with the implementation.

2012-05-31

Hai Bui answers:

Hi,

Wordpress provides fetch_feed function to retrieve an external feed and parse it. Is it what you need?
http://codex.wordpress.org/Function_Reference/fetch_feed

<?php // Get RSS Feed(s)
include_once(ABSPATH . WPINC . '/feed.php');

// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed('http://example.com/rss/feed/goes/here');
if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly
// Figure out how many total items there are, but limit it to 5.
$maxitems = $rss->get_item_quantity(5);

// Build an array of all the items, starting with element 0 (first element).
$rss_items = $rss->get_items(0, $maxitems);
endif;
?>

<ul>
<?php if ($maxitems == 0) echo '<li>No items.</li>';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>
<li>
<a href='<?php echo esc_url( $item->get_permalink() ); ?>'
title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>
<?php echo esc_html( $item->get_title() ); ?></a>
</li>
<?php endforeach; ?>
</ul>


Lucian Florian comments:

I tried your code and I get a message text "no items" even if I tried to raise the max limit.

Here is the HTML (of course images, title, times etc will need to be pulled from the feed):


<!-- listing -->
<div class="listing-wrap clearfix">
<a href="i/pop-up-img.jpg" class="show-thumb preview"><img src="i/show-listing-thumb.jpg" alt="show-listing-thumb" width="50" height="75" /></a>
<div class="show-listing">
<h2 class="show-title">21 JUMP STREET (2012)</h2>
<span class="smalltext darkgrey">110 mins R Digital</span>
<ul class="show-times clearfix">
<li>2:20 </li>
<li><a href="#">4:55</a></li>
<li><a href="#">5:30</a></li>
<li><a href="#">7:05</a></li>
<li> <a href="#">8:30</a></li>
<li><a href="#">11:40</a></li>
</ul>
</div>
</div>
<!-- listing -->


Hai Bui comments:

Oh, sorry, I've just realized that function won't work for you. You should go with John's solution. If you need to sort/filter the items, you have to loop through the items and store them into an array.