Dynamically replacing sushi while parsing an rss feed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paulshort
Forum Newbie
Posts: 4
Joined: Sat Mar 29, 2008 10:26 pm

Dynamically replacing sushi while parsing an rss feed

Post by paulshort »

Hi everyone,

This is my first post here at devnetwork and I come to ask for help with a script modification I can NOT seem to get to work.

I am not very knowledgable in PHP and am in the process of learning, but after 3 days of searching and reading up online, I still can't seem to get this to do what I want it to. I know it's possible and the solution is probably so simple I'll be kicking myself when it finally works.

Anyway, hello to you all and I hope someone here will be kind enough to help me out with this.

First of all, here is the script in it's original working form. This script uses the built in rss parser in wordpress and the code below outputs a list of 10 headlines from just about any rss feed. In the example below, I've included a Google News Search feed for the word "sushi".

See the rss feed url on line 4 with the word sushi in it.

Code: Select all

<?php
require_once (ABSPATH . WPINC . '/rss-functions.php');
// insert the feed URL here
$rss = @fetch_rss('http://news.google.com/news?hl=en&ned=&q=sushi&ie=UTF-8&output=rss');
if ( isset($rss->items) && 0 != count($rss->items) ) {
?>
<?php
// set the number of items from the feed to display (10)
$rss->items = array_slice($rss->items, 0, 10);
foreach ($rss->items as $item ) {
?>
<li>
<a href='<?php echo wp_filter_kses($item['link']); ?>'>
<?php echo wp_specialchars($item['title']); ?></a>
</li>
<?php } ?>
<?php } ?>
Now, here's what I want to do to modify the script a bit.

I want to replace the word "sushi" with a php tag that will insert the title of my blog post in there.

So I'll use this tag:

Code: Select all

<?php wp_title(); ?>
...and the google news feed url will now look like...

Code: Select all

$rss = @fetch_rss('http://news.google.com/news?hl=en&ned=&q=<?php wp_title(); ?>&ie=UTF-8&output=rss');
but it simply doesn't work and I've tried everything I know (and that's not much).

Can someone please make some suggestions as to how to dynamically insert the output of the wp_title tag into the middle of that url?

Thank you in advance for any help or suggestions you may offer.

If I figure it out for myself I'll post my modified code here.

Paul
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Dynamically replacing sushi while parsing an rss feed

Post by John Cartwright »

Code: Select all

$rss = @fetch_rss('http://news.google.com/news?hl=en&ned=&q='. urlencode(wp_title()) .'&ie=UTF-8&output=rss');
:wink:
paulshort
Forum Newbie
Posts: 4
Joined: Sat Mar 29, 2008 10:26 pm

Re: Dynamically replacing sushi while parsing an rss feed

Post by paulshort »

Hi Jcart,

Thanks for the quick reply and I see where you're going with it, but for some reason, the code snip above output the title once in the list and then underneath that it outputs a list from the default google news feed like this
  • Sushi
  • Obama: Clinton welcome to stay in race - Reuters
  • In Iraq, US caught in middle of Shiite rivalry - Los Angeles Times
  • Zimbabwe opposition claims election win on early results - ABC Online
etc.

The title gets listed first and then the rest of the list is the default google news feed as if there were no search term inserted into the url before it's parsed.

:(

Paul

P.S. You're in Toronto? I'm in Whitby!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Dynamically replacing sushi while parsing an rss feed

Post by Chris Corbyn »

paulshort wrote:P.S. You're in Toronto? I'm in Whitby!
Whitby as in English seaside Whitby? I'm from Guisborough :)

EDIT | I should probably answer your question as well as saying Hi. It sounds like wp_title() actually echo()'s content rather than returning a value.
paulshort
Forum Newbie
Posts: 4
Joined: Sat Mar 29, 2008 10:26 pm

Re: Dynamically replacing sushi while parsing an rss feed

Post by paulshort »

Hi Chris,

I'm actually in Whitby, Canada, which is about a half hour east of Toronto.

Yes, wp_title() echo()'s the title of the article in wordpress. Is there some other way I can insert it in there?

How about if I somehow whip up a bit of code that turns the output of wp_title() into a value and then pull that in using urlencode() ?

Hmmm...

Paul
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Dynamically replacing sushi while parsing an rss feed

Post by s.dot »

how about..

Code: Select all

<?php
ob_start();
wp_title();
$wp_title = urlencode(ob_get_contents());
 
//now user $wp_title in place of wp_title();
That would be if wp_title() is echoing the contents. Seems ugly, but I can't think of another way to capture it if it's not returning anything.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
paulshort
Forum Newbie
Posts: 4
Joined: Sat Mar 29, 2008 10:26 pm

Re: Dynamically replacing sushi while parsing an rss feed

Post by paulshort »

Scottayy, it worked!!! :D

The keyword is being inserted into the rss newsfeed url right where it should be now, and the list is displaying 10 on-topic headlines.

Only thing is. I'm still getting the title showing up as a result of line 3 in your code example, but I can do something to hide that with css on the page.

Thanks a million, Scottayy!! :wink:

Paul

P.S. Here's the finished product in case anyone wants to use it for their own wordpress blog.

Code: Select all

<h4>News</h4>
<ul class="ext-links">
<?php
require_once (ABSPATH . WPINC . '/rss-functions.php');
ob_start();
the_title();
$the_title = urlencode(ob_get_contents());
// insert the feed URL here
$rss = @fetch_rss('http://news.google.com/news?hl=en&ned=&q='. urlencode($the_title) .'&ie=UTF-8&output=rss');
if ( isset($rss->items) && 0 != count($rss->items) ) {
?>
<?php
// set the number of items from the feed to display (10)
$rss->items = array_slice($rss->items, 0, 10);
foreach ($rss->items as $item ) {
?>
<li>
<a href='<?php echo wp_filter_kses($item['link']); ?>'>
<?php echo wp_specialchars($item['title']); ?></a>
</li>
<?php } ?>
<?php } ?>
</ul>
Note: I made a mistake in my code in the original post in this thread. I used wp_title() instead of the_title(). wp_title() only works in the <head></head> of a wordpress page or post, whereas the_title() works in the body of the page itself. I realized it after I had made the OP. Both give the same output just in different areas of the page.

Thanks again everyone for your help. Even though some things didn't work, I still have a better understanding of php because of your help.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Dynamically replacing sushi while parsing an rss feed

Post by Chris Corbyn »

ob_get_contents() doesn't clear the buffer so you need this:

Code: Select all

<?php
ob_start();
wp_title();
$wp_title = urlencode(ob_get_clean());
Post Reply