RSS Feed Issue

Hi, I am a complete beginner here but have been following a couple of tutorials online to create a page in a mobile app to display a blog’s posts but I can’t get it to work. any help would be greatly appreciated.

Here’s the code for the page:

[code]<?php

$location = “http://query.yahooapis.com/v1/public/yql?q=”;
$location .= urlencode(“SELECT * FROM feed WHERE url=‘http://bibacity.wordpress.com/feed/’”);
$location .= “&format=json”;
$rss = file_get_contents($location, true);
$rss = json_decode($rss);
?>

Bibacity Blog
  <ul data-role="listview">
  <?php
   foreach($rss->query->results->item as $post)
  {
	  $title = $post->title;
	  $link = $post->link;
	  $desc = $post->description;
	  $img = $post->enclosure->url;
 echo "<li><a href=\"$detail.php?title=$title&desc=$desc\" rel=\"external\"><img src=\"$img\"/><h3>$title</h3></a></li>";
 
 }
 
  ?>
  </ul>
  </div>
[/code]

Well, give us an idea on what is not working and we may be able to help.
Did you debug the code at all? One way is to first make sure the location you are pulling the RSS feed from is a valid address. You can add some PHP code to the page you showed us to debug the location.

Add: die($location); Just before the end of the location code. This will cause the PHP code to end with a display of the actual location you are trying to pull data from. Then, look at that display and see if it is correct.
Even one space will cause the location URL to throw an error.

Next, you can debug what it is pulling from that location, if the URL is correct, by adding this line to the end of the PHP code: $die($rss); which will kill the PHP code and display the data in the $rss that is pulled from the URL.

In this way, you can debug that the data you are receiving is valid. If so, then, the problem is in the way that you are displaying the data in the $rss variable.

Since you did not tell us what you can not get to work, I hope these help you locate where the code is breaking down.

Let us know if you solve it.

Thanks for the help, but I think I might be in a bit over my head here. I think I need to go back and learn some basics before tackling this.

The problem, by the way, was that the code after “foreach” was displayed as text on the live page, which would indicated a problem with the $rss string?

Thanks again for your reply.

Well, RSS feeds are STRINGS! They are simple, just like TWEETS. Just text sent out so that you can display them in a banner-type display. They, can include link anchors so people can click on them, but, they are just text. Did you think of them as something else?

Also, the RSS feeds are text feeds given to you and you have to create the rotating banner display for them.

Here is a very complete and simple to understand tutorial on RSS feeds. It shows small amounts of info and you press the “Next Chapter” links in green and you will get the next step. It explains it in all details. Hope that helps! http://www.w3schools.com/rss/

I think the RSS feed itself is fine (its from wordpress), but the PHP code to fetch and display it is not working as it’s being displayed as text on the page. Any help is very much appreciated but I think my lack of knowledge and experience may mean I’ll have to leave this for a bit.

Well, in that case, you should read thru the tutorial I posted for you. It shows how RSS works and how to display it. If you are already reading the RSS feed, then, you just have to decide how to display it. Since RSS feeds are just text, you can create a

and slide the text across the area.

When you get back to it, let us know and someone here can help you.

So, the issue there was that I hadn’t setup a server to test the PHP so it was treating it as html. The RSS feed is now working and returning results, the problem I know have is displaying those results as I want and I’m not sure if this is the forum for that question but here goes. Currently, the post titles are being displayed as a list (using JQuery Mobile in Dreamweaver) and when you click on each post it brings up a page with just the title. If I configure it to display the content of the post, it displays on the list page and not on the individual post page. How do I configure it so that it shows just the titles as a list and then you click through to the content? It seems that it will only display an item from the RSS on BOTH the link page AND individual page, not one or the other. Again, any help is appreciated, and if this isn’t the right place then any direction would also be helpful.

Code for the reader.php:

[code]<?php

$location = “http://query.yahooapis.com/v1/public/yql?q=”;
$location .= urlencode(“SELECT * FROM feed WHERE url=‘http://bibacity.wordpress.com/feed’”);
$location .= “&format=json”;
$rss = file_get_contents($location, true);
$rss = json_decode($rss);
?>

Bibacity Blog
    <?php foreach($rss->query->results->item as $post)

    {

    $title = $post->title;
    $link = $post->link;
    $detail = $post->description;
    $content = $post->encoded;

    echo “

  • <a href=“detail.php?title=$title”>

    $title

  • ”;

    }

    ?>

[/code]

And code for the detail.php which each title links to:

[code]<?php

$location = “http://query.yahooapis.com/v1/public/yql?q=”;
$location .= urlencode(“SELECT * FROM feed WHERE url=‘http://bibacity.wordpress.com/feed’”);
$location .= “&format=json”;
$rss = file_get_contents($location, true);
$rss = json_decode($rss);
?>

Bibacity Blog
    <?php foreach($rss->query->results->item as $post)

    {

    $title = $post->title;
    $link = $post->link;
    $detail = $post->description;
    $content = $post->encoded;

    echo “

  • <a href=“detail.php?title=$title”>

    $title

  • ”;

    }

    ?>

[/code]

Did you go thru the tutorial that I posted from the W3 site? It explains that RSS feeds are sent in XML format.

This means you have for each part of a RSS feed. You can strip out whatever info you want from the feed and display it using any code language you want to use.

In your code, you pull the entire RSS feed into an array called $rss. Next, you beak up each separate feed into an array called $post. So, you have the “title, link, description and contect” and load them into variables.

So, with each post already broken down into the variables: $title, $link, $detail, $content, you can just display them in any manner you wish. Now, you say:

Currently, the post titles are being displayed as a list (using JQuery Mobile in Dreamweaver) and when you click on each post it brings up a page with just the title.
So, you just have to find in the code where the title and add in the other variables. If the "detail.php" code is where it is, you currently have the line like this:

echo “

  • <a href=“detail.php?title=$title”>

    $title

  • ”;

    So, that PHP command does display the title only. If this is where you need to change it, you can add the other sections there. Something like:

    echo “

  • <a href=“detail.php?title=$title”>

    $title

    (details=’$details’)
  • ”;
    (Just an example of adding in the details on the line.)

    Since this line is inside of

  • tags, they are in a “list”, so quite often you will have to format it using CSS to make the details the correct size on the screen, etc. So, it is possible to do whatever you want, you just need to sort it all out.

    Not sure if this is what you were asking for. Hope it helps…

  • Yep, read that tutorial and I understand the basics of RSS. The list page is good, all I want showing is the title. And I can sort the formatting and CSS later. The main problem at the moment is that when the title button is pressed, it should link to the full posts, coded as $content. Instead, it just links to a page with the title. So this is related to the detail.php page. That’s what I can’t work out.

    Thanks for your help so far, I’m learning lots!

    Good! Now I understand what you want. So, I gave you the answer. Your second post was the detail.php file.

    In there your code only echo’s (print’s) the title. It is set to only display the title. That is how it is coded.
    So, the change I showed you would display a little more. You need to alter the echo command to display
    whatever you want it to. Right now it just displays the title. So, the code is doing what it was coded for.

    Change that line.

    Also, in your reader.php file, you post to the detail.php file using this code:

    echo “

  • <a href=“detail.php?title=$title”>

    $title

  • ”;

    So, you are using “title=” as the calling option. Usually, in PHP if a link calls a routine in another file using a parameter then the receiving file (detail.php) would use this info to pull the info using that parameter.

    To do that, you use a $_GET command. So, normally, you would do something like:
    $title=$_GET[‘title’];
    Which would grab the value passed to it. Then, you would use that value to query the DB and pull the correct data from it. You are using some sort of code to pull the info, but, I do not quite understand what it is doing:

    $location = “http://query.yahooapis.com/v1/public/yql?q=”;
    $location .= urlencode(“SELECT * FROM feed WHERE url=‘http://bibacity.wordpress.com/feed’”);
    $location .= “&format=json”;
    $rss = file_get_contents($location, true);
    $rss = json_decode($rss);

    This code loads a file and sorts it into an RSS feed of sorts. But, it does not get the one that you are feeding to it. So, to fix that part up would involve a modification in the for loop. You would have to check to see if the current one is the one that was selected. Then, you would need to display all the data for it. This is one sample of how it would work. (Might not be the best way, but, should work.)

    OLD version:
    [php]

    <?php foreach($rss->query->results->item as $post) { $title = $post->title; $link = $post->link; $detail = $post->description; $content = $post->encoded; echo "
  • $title

  • "; } ?>

    [/php]

    Altered version with added items:
    [php]

    <?php $selected_title = $_GET['title']; // Grabs the one sent to it... foreach($rss->query->results->item as $post) { $title = $post->title; $link = $post->link; $detail = $post->description; $content = $post->encoded; if ($title==$selected_title) echo "

    $title



    "; echo $details . "

    ; echo "------------------------------

    "; echo $content; } ?>

    [/php]
    So, as you see, it grabs the selected title. Then, loads the RSS feed. Then, parse thru each feed until it sees the selected title and then displays some items from it.

    Hopefully that is what you are looking for. Good luck!

    Thanks for that, but I realised I made a mistake, I posted the reader.php twice. Here is the detail.php that it links to:

    [code]

    detail <?php $title = $_GET['title']; $detail = $_GET['detail']; $content = $_GET['content']; ?>

    <?=$title;?>

    <?=$content;?>

    Back
    </div>
    
    [/code]

    So this is what I need altered so that it shows the title and the content. What is displayed on the reader.php when it is live is exactly as I want it.

    Okay, now I understand what you need help with! Well, this is a simple fix…

    In the first code you posted that creates the link to the details page:

    echo “

  • <a href=“detail.php?title=$title”>

    $title

  • ”;

    You notice this sends the title only to the details page. But, in the details page code you read the details and the content from this line. Since it is not sent, it can never be read. So, either you must redo the details page to re-read the feed to get this info or just pass it in the above link as the details page currently expects. Something like this:

    echo “

  • <a href=“detail.php?title=$title&detail=’$detail’&content=’$content’”>

    $title

  • ”;

    This adds the two other parts of the feed into the link itself. This is not the best way to do it as the text could be very large and may cause issues if special chars are sent in the text. But, that is for another post.

    Just for extra comments, you can pass this info thru a $_SESSION variable which would be much safer and much more secure. Since $_SESSION variables are not passed thru a URL link, they can not be seen. (All items passed thru a “HREF” are seen by the user and can be altered by a hacking program or tricky user…)

    So, hope that helps… Good luck!

    Sponsor our Newsletter | Privacy Policy | Terms of Service