PHP - Rss Feed Item Showing Twice
Hey all,
I'm currently making myself a blog website from scratch. It's sort of my personal project; I'm using it to get a feel for PHP (and it'd look great on my resume!). Anyway, I'm using the following PHP code to get the items from my RSS feed, and display them. Problem is, my first item shows twice! I'm thinking it's a problem with my loop, but I'm not sure. Any ideas: My PHP code to render my RSS is as follows: <?php //Purpose: This loads the XML document using the DOMParser, and mixes it w/ HTML to style and format it. try { $xmldoc = new DOMDocument(); $xmldoc->load('test_RSS_feed.xml'); $content = null; //HTML mixed with XML results go here $allitems = $xmldoc->getElementsByTagName('item'); foreach ($allitems as $item) { //get ALL instances of wanted element(s) $titles = $item->getElementsByTagName('title'); $dates = $item->getElementsByTagName('pubDate'); $descriptions = $item ->getElementsByTagName('description'); //will include the CSS classes for these eventually! $content.= (' <ul class ="blog_entry"> <li class="title_entry"> '.$titles->item(0)->nodeValue. '</li> <li class="date_entry"> '.$dates->item(0)->nodeValue. '</li> <br> <li class="description_entry"> '.$descriptions->item(0)->nodeValue. '</li> </ul> <br><br><br>'); echo $content; } } catch(Exception $e) { echo $e->getMessage(); } ?> And the XML in my RSS feed is as follows: <?xml version= "1.0" encoding = "UTF-8" ?> <!--Every XML doc starts with this --> <?xml-stylesheet type="text/css" href="sample_xml_css.css" ?> <!--stylesheet for RSS feed--> <rss version = "2.0"> <channel> <!--RSS feed information--> <title>My first RSS feed</title> <description>This is my first foray into RSS. I may use this to create a blog!</description> <lastBuildDate>Mon, Oct. 11, 2010 13:13:26 GMT</lastBuildDate> <!--date format complies to different standards--> <webMaster>mserrano0410@gmail.com</webMaster> <!--has email of webmaster--> <item> <title>My first item; this is the title.</title> <link>http://www.example.com/sample_item.html</link> <!--provides a link to the RSS item; required--> <pubDate>Mon, Oct 11, 2010 13:15:20 GMT</pubDate> <!--Every date complies to formatting standards--> <description>We are now inside of an item; this is a description tag.</description> </item> <item> <title>My second item.</title> <link>http://www.google.com</link> <pubDate>Fri, Oct 15, 2010 23:02:40 GMT</pubDate> <description>This is a second helping of examples biatch!</description> </item> <!--can create as many items as you want, but it MUST have: 1) Title 2) Link to item; when clicked, takes you directly to that item. This is a SEPARATE HTML document! 3) Description --> </channel> </rss> Thanks in advance, Marvin Similar TutorialsIf you goto http://www.actionfx.net/bfd/photos.php you will see that im trying to show an rss feed from http://picasaweb.google.com/data/feed/base/user/114565750484201639035?alt=rss&kind=album&hl=en_US&access=public My code works well if its just text on the rss but it does not work on this particular rss. I want the photos to show up on my page also. Im hoping someone here can be so kind to help me out with this coding. Below is my code. Code: [Select] <?php $feed_url = "http://picasaweb.google.com/data/feed/base/user/114565750484201639035?alt=rss&kind=album&hl=en_US&access=public"; // INITIATE CURL. $curl = curl_init(); // CURL SETTINGS. curl_setopt($curl, CURLOPT_URL,"$feed_url"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0); // GRAB THE XML FILE. $xmlTwitter = curl_exec($curl); curl_close($curl); // SET UP XML OBJECT. // Use either one of these, depending on revision of PHP. // Comment-out the line you are not using. //$xml = new SimpleXMLElement($xmlTwitter); $xml = simplexml_load_string($xmlTwitter); // How many items to display $count = 10; // How many characters from each item // 0 (zero) will show them all. $char = 100; foreach ($xml->channel->item as $item) { if($char == 0){ $newstring = $item->description; } else{ $newstring = substr($item->description, 0, $char); } if($count > 0){ //in case they have non-closed italics or bold, etc ... echo"</i></b></u></a>\n"; echo" <div style='font-family:verdana; font-size:.12;'> <b>{$item->title}</b><br /> $newstring ... <span class='redlink' id='redlink' style='redlink'> <a href='{$item->guid}'>read more</a> </span> <br /><br /> </div> "; } $count--; } ?> I have a script that seems to work well to insert a bookmark into a users database when he/she is logged into the system but I am having a hard time figuring out how I would go about making a work-a-round for having an item selected before being logged in, and inserted after they have logged in or registered. For example, I would like a user to be able to select an Item to add to bookmark whether that user is logged in/registered or not and if they are not, they would be greeted with a login/registration form and after successful login the add bookmark script would be initiated on the item previously selected. What I've got this far: Simple form to add bookmark: <form name="bm_table" action="add_bms.php" method="post"> <input type="text" name="new_url" value="http://" /> <input type="submit" value="Add Bookmark"/> </form> Then I have the add bookmark script: BEGIN php $new_url = $_POST['new_url']; try { check_valid_user(); //cannot get past this part since it ends the script....code below if (!filled_out($_POST)) { throw new Exception('Form not completely filled out.'); } // check URL format if (strstr($new_url, 'http://') === false) { $new_url = 'http://'.$new_url; } // check URL is valid if (!(@fopen($new_url, 'r'))) { throw new Exception('Not a valid URL.'); } // try to add bm add_bm($new_url); echo 'Bookmark added.'; // get the bookmarks this user has saved if ($url_array = get_user_urls($_SESSION['valid_user'])) { display_user_urls($url_array); } } catch (Exception $e) { echo $e->getMessage(); } END php Checking valid user - the portion I cannot get past in the above script: function check_valid_user() { // see if somebody is logged in and notify them if not if (isset($_SESSION['valid_user'])) { echo "Logged in as ".$_SESSION['valid_user'].".<br />"; } else { // they are not logged in do_html_heading('Problem:'); echo 'You are not logged in.<br />'; do_html_url('login.php', 'Login'); do_html_footer(); exit; } } How would I go about modifying the script so that a user could fill in the form (later it would be a link...obviously they probably wouldn't be filling in a form that is log-in specific - but same concept I think) Thanks in advance for the help! tec4 Hi there, I think this is a big question but I'd appretiate any help you can provide!! I have a list of items and subitems in a table that looks like this: id parent_id title 1 0 House Chores 2 1 Take Out Trash 3 1 Clean Room 4 0 Grocery List 5 4 Eggs 6 4 Produce 7 6 Lettuce 8 6 Tomato 9 4 Milk I want to display it like this: (+) House Chores: > Take Out Trash > Clean Room (+) Grocery List: > Eggs (+) Produce > Letutce > Tomato > Milk So basically each entry in the table has an unique id and also a parent id if it's nested inside another item. I "sort of" got it figured out in one way, but it doesnt really allow for nested subgroups. I'd like to know how would y'all PHP freaks to this Also taking suggestions for the javascript code to expand/collapse the tree !! Thank you! Well I am looking to change this url Code: [Select] http://website.com/product.php?Item=2369 to Code: [Select] http://website.com/product.php?Item=Item-Name Heres a snip of the code that handles that. <?php include_once('mysql_connect.php');$id = (int)$_GET['Item'];?>() any help would be appreciated. I am pretty new to PHP and am trying to create a simple (so I assumed) page to takes data from one html page(works fine) and updates a MYSQL Database. I am getting no error message, but the connect string down to the end of the body section is showing up as plain text in my browser window. I do not know how to correct this. I have tried using two different types of connect strings and have verified my names from the HTML page are the same as listed within the php page. Suggestions on what I need to look for to correct would be great. I have looked online, but so far all I am getting is how to connect, or how to create a comment, so I thought I would try here. Thank you for any assistance I may get!! - Amy - Code: [Select] <body><font color="006600"> <div style="background-color:#f9f9dd;"> <fieldset> <h1>Asset Entry Results</h1> <?php // create short variable names $tag=$_POST['tag']; $serial=$_POST['serial']; $category=$_POST['category']; $status=$_POST['status']; $branch=$_POST['branch']; $comments=$_POST['comments']; if (!$tag || !$serial || !$category || !$status || !$branch) { echo "You have not entered all the required details.<br />" ."Please go back and try again."; exit; } if (!get_magic_quotes_gpc()) { $tag = addslashes($tag); $serial = addslashes($serial); $category = addslashes($category); $status = addslashes($status); $branch = addslashes($branch); $comments = addslashes($comments); } //@ $db = new mysqli('localhost', 'id', 'pw', 'inventory'); $db = DBI->connect("dbi:mysql:inventory:localhost","id","pw") or die("couldnt connect to database"); $query = "insert into assets values ('".$serial."', '".$tag."', '".$branch."', '".$status."', '".$category."', '".$comments."')"; $result = $db->query($query); if ($result) { echo $db->affected_rows." asset inserted into Inventory."; } else { echo "An error has occurred. The item was not added."; } $db->close(); ?> </fieldset> </div> </body> Hi I get an error when trying to add a link to my rss feed Code: [Select] <link>http://www.jobjar.co.uk/jobdetails.php?keywords=lon0014&page=1</link>Something to do with the last part of the link because when I remove the page=1 it then works. How can I resolve this? Also, will I have to manually update my xml file for rss feeds or can it be updated automatically? If so how? Thanks I'm lost on this one. This code works fine in Firefox, but not my IE8. I think it has something to do with the <?php echo '<?xml version="1.0" encoding="utf-8"?>'; ?> line, but i'm not sure. IE says "Internet Explorer cannot display this feed" and "A name contained an invalid character. Line: 7 Character: 701." Any ideas? <?php header('Content-Type: text/xml'); ?> <?php echo '<?xml version="1.0" encoding="utf-8"?>'; ?> <?php include('../vars.php'); ?> <rss version="2.0"> <channel> <title>Newsfeed</title> <description>News and Updates</description> <link>http://www.example.com</link> <language>en-us</language> <?php require_once("../conn.php"); $query = "SELECT * FROM news"; $data = mysqli_query($db, $query); while ($row = mysqli_fetch_array($data)) { echo '<item>'; echo ' <title>' . $row['title'] . ' - ' . substr($row['content'], 0, 32) . '...</title>'; echo ' <link>http://www.example.com/new.php?newsid=' . $row['newsid'] . '</link>'; //echo ' <pubDate>' . $row['date'] . '</pubDate>'; echo ' <description>' . $row['content'] . '</description>'; echo '</item>'; } ?> </channel> </rss> Hi All, I'm trying to incorporate a BBC Sport RSS Feed into my website. The following code will show me the news feeds as text and I can include the url's as text also but want to turn this into a clickable url. How do I do this, I cant work it? please help. $feed_url = "http://newsrss.bbc.co.uk/rss/sportonline_uk_edition/football/teams/s/shrewsbury/rss.xml"; // Get data from feed file $response = file_get_contents($feed_url); // Insert XML into structure $xml = simplexml_load_string($response); // Browse structure foreach($xml->channel->item as $one_item) echo $one_item->title."<BR>"; I'm been searching and can't seem to figure this out. I want to take an RSS feed from a news site and display it on my site. any help? Hello! I'm trying to develop some kind of RSS news aggregator and I want to show only feeds younger than 1day. I figured i could transform RSS pubDate to timestamp (strtotime()), but there are some feeds without timestamp (like: http://izklop.com/xmldata/rsslinks.xml). Is there any other way to do it, so I could find timestamp from those feeds without pubdate? If there isn't any other way, do You think it is ok, to just show last 5 feeds? I hope I made my self clear, and please forgive me for my English hi i want to add some content to my site using xml rss i have a feed i want to show, and the only thing i can find to show it on my page or widgets but they dont look like i want it to look, so i want to make a php script that reads the xml file and then parses it into my website dont know if this is even possible ... but if its possible, is it also possible to filter the feed on certain keywords? I have tried sitemap.org and a few others but have not found anything that will give the results I am after. I have a blog site that has a URL and a Description for the URL and wish to have the description show as the anchor text which is linked to the URL, all data is taken from MySQL. Does anyone have a code that will generate something like this ? Hello all That forum is my last desperate attemp to do what i want to do. Ok here is the story I want to create a simple rss feed in conjuction with php and mysql. I dont want admin areas ect , i just want when i insert a new listing to my database to be able shown up to my (future) rss subscribers. To be more technically specific i want to show to my surfers updates about 2 tables in my database not all the tables. The example i found so far were about only 1 table, plus i was encounting errors to my script. I would like some ideas, directions if someone is kind enough to help a sad developer Thanks in advance! hi all, i am trying to just get a couple things from this RSS feed: http://www.sierraavalanchecenter.org/bottomline-rss.php I would like to grab the danger rose image on the left, the text that tells of the Avalanche danger ie... "Avalanche Danger Remains LOW..." and the date. I have tried using a few things like: $html = 'http://www.sierraavalanchecenter.org/bottomline-rss.php';$dom = new DOMDocument;$dom->loadHTML($html);$xpath = new DOMXPath($dom);$res = $xpath->query('//div[@id="dangericon"]');if ($res->item(0) !== NULL) { $test = $res->item(0)->nodeValue;}() ...without any luck. Anyone have some advice for me? thanks! Hi, I've written some code to take information from an SQL database and write it out in the RSS format (Although it doesn't validate). The problem is i'd like the page to have the .rss (or .xml) file extension, I'm not sure if there's any advantages in having this but thought i'd ask. I've got the following code: Code: [Select] <?php header('Content-type: text/xml'); print '<?xml version="1.0"?>'; print '<rss version="2.0">'; print '<channel>'; include("phpfunctions.php"); db_connect(); //select all from users table $select="SELECT title, link, description FROM news"; $result = mysql_query($select) or die(mysql_error()); //If nothing is returned display error no records if (mysql_num_rows($result) < 1) { die("No records"); } //loop through the results and write each as a new item while ($row = mysql_fetch_assoc($result)) { $item_title = $row["title"]; $item_link = $row["link"]; $item_desc = $row["description"]; print '<item>'; print '<title>' . $item_title . '</title>'; print '<link>' . $item_link . '</link>'; print '<description>' . $item_desc . '</description>'; print '</item>'; } print '</channel>'; print '</rss>'; ?> This seems to work fine as i get what i expect and i'm assuming i can do the same to output .xml but is there a way to have it in a proper .rss / .xml file so that an aggregator or someone could read this properly. Cheers, Reece I have a php script that saves some data as a .xml document. When I view this in my browser I can subscribe to the feed (I am using firefox). However I asked someone else to see if they could subscribe (they were using IE) but the couldn't. Therefore how can users subscribe to my feed? Will only some be able to subscribe? Thanks for any help. Hi, I have a small piece of code that creates an RSS feed using a mysql database. The issue is the page itself is blank however if I right click and view source I can see all the feed there. I convert the dat time field into a standard RSS date field. The web address is http://vinovote.com/news/feed.php My code is as follows Code: [Select] <?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?> <rss version="2.0"> <channel> <title>Vinovote.com</title> <description>The Latest News And Views From Around The Web</description> <link>http://www.vinovote.com/</link> <copyright>Your copyright information</copyright> <?php require_once('../Connections/connection.php'); mysql_select_db($database_vinovotedb, $vinovotedb); $doGet = mysql_query("SELECT feed_content.feed_content_id, feed_content.feed_id, feed_content.url, feed_content.title, feed_content.content, feed_content.item_time, Date_FORMAT(feed_content.item_time,'%a, %d %b %Y %T') AS pubDate FROM feed_content order by item_time desc LIMIT 50 ", $vinovotedb) or die(mysql_error()); while($result = mysql_fetch_array($doGet)){ ?> <item> <title> <?php echo $result['title']; ?></title> <description> <?php echo $result['content'];?></description> <link><?php echo $result['url'];?></link> <pubDate> <?php echo $result['pubDate']; ?></pubDate> </item> <?php } ?> </channel> </rss> I'm not sure you can do this but i would like to grab the image gif from the rss feed of yahoo weather. For instance http://weather.yahooapis.com/forecastrss?p=77056 I pulled this script from a tutorial online and it works great for displaying the temperature and condition and i realize those values are located within the xml. But i would like to grab the image located between the <description> tags (if you look at the source of the rss feed). I don't know if this is possible or not. Thanks. Code: [Select] <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Yahoo! Weather API RSS</title> <?php function retrieveYahooWeather($zipCode="92832") { $yahooUrl = "http://weather.yahooapis.com/forecastrss"; $yahooZip = "?p=$zipCode"; $yahooFullUrl = $yahooUrl . $yahooZip; $curlObject = curl_init(); curl_setopt($curlObject,CURLOPT_URL,$yahooFullUrl); curl_setopt($curlObject,CURLOPT_HEADER,false); curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true); $returnYahooWeather = curl_exec($curlObject); curl_close($curlObject); return $returnYahooWeather; } $localZipCode = "77056"; // Houston, Tx $weatherXmlString = retrieveYahooWeather($localZipCode); $weatherXmlObject = new SimpleXMLElement($weatherXmlString); $currentCondition = $weatherXmlObject->xpath("//yweather:condition"); $currentTemperature = $currentCondition[0]["temp"]; $currentDescription = $currentCondition[0]["text"]; ?> </head> <body> <h1>Houston, TX</h1> <ul> <li>Current Temperatu <?=$currentTemperature;?>°F</li> <li>Current Description: <?=$currentDescription;?></li> </ul> </body> </html> |