Handling different RSS Feed Formats - php

I am trying to create a personal job board using RSS feeds from Craigslist, Reddit, Kijiji, and Indeed.
I have found a method (using magpie) to bring in the multiple feeds, however I am not able to parse any data from Indeed.ca. I tried echoing the results at different stages, to make sure Iw as connected to the RSS Feed for Indeed, and I was able to get information, but it won't display on the finished product.
Here's my code to call the RSS Feeds (rss-urls.php):
$urls = array(
//Craigslist RSS Feeds
'http://toronto.en.craigslist.ca/med/index.rss',
//Reddit RSS Feeds
'http://www.reddit.com/r/forhire/new/.rss',
//Kijiji RSS Feeds
'http://www.kijiji.ca/rss-srp-graphic-web-design-jobs/owen-sound/c152l1700187',
//Indeed RSS Feed
'http://www.indeed.ca/rss?q=Graphic+Designer&l=Toronto%2C+Ontario');
foreach($urls as $url) {
$rss = fetch_rss($url);
foreach ($rss->items as $item ) {
$title = $item[title];
$url = $item[link];
$description = $item[description];
$date = $item['dc']['date'];
//print_r($tot_array);
rsort($tot_array);
And here's the code that takes the feed's info and displays it:
foreach($tot_array as $tot) {
$all = explode(",",$tot);
$date = date("Y-m-d",strtotime($all[4]));
$now = date("Y-m-d");
$title = $all[1];
$url = $all[2];
$description = $all[3];
//echo $tot."";
//print $url;
if (false !== strpos($url,'indeed')) {
echo '<div id="linkCell" style="width: 100%;">';
echo '<div id="vAlign">';
echo '<p class="linkTitle">'.$title.'</p><br />';
//echo '<span class="date">Post is '. date_diff(date_create($date), date_create($now))->format('%a day(s) old') .'</span></p>';
echo '<p class="description">'.$description.'</p>';
echo '</div>';
echo '</div>';
echo '<span style="color:white;">'.$date."</span><br>";
}
}

If you run the result on a browser, you can see there are lots of empty lines from Indeed RSS feed.
I would trim out those lines before parsing it.

Related

Convert a RSS feed from php to xml

The code below is a PHP generator that generate a simple web page in PHP with just one feed from my RSS URL. This URL has around 60 feeds.
I would like to generate a XML RSS with just one feed in random mode. Is this possible?
<?php
function load_xml_feed($feed){
global $RanVal;
$i= 1;
$FeedXml = simplexml_load_file($feed);
foreach ($FeedXml->channel->item as $topic) {
$title[$i] = (string)$topic->title;
$link[$i] = (string)$topic->link;
$description[$i] = (string)$topic->description;
$i++;
}
$randtopic = rand(2, $i);
$link = trim($link[$randtopic]);
$title = trim($title[$randtopic]);
$description = trim($description[$randtopic]);
$RanVal = array($title,$link,$description);
return $RanVal;
}
$rss = "http://syncds.boxip.com.br/feed?code=24C165DB04";
load_xml_feed($rss);
$link = $RanVal[1];
$title = $RanVal[0];
$description = $RanVal[2];
echo "<h1>".$title."</h1><h2>".$link."</h2><p>".$description."</p>";
?>
Thank you guys.

How parse RSS feed

I'm trying to parse an RSS feed in PHP for the first time. It seems to go fine until I actually try to display anything! This example is me trying to pull out four random organization names from the feed (I actually want to display more, but am keeping it simple here...)
$xml = file_get_contents('https://rss.myinterfase.com/rss/oxford_RSS_Jobs_xml.xml');
foreach($xml->Row as $job) {
$item[] = array(
'OrganizationName' => (string)$job->OrganizationName,
'job_JobTitle' => (string)$job->job_JobTitle,
'job_expiredate' => strtotime($job->job_expiredate),
'ExternalLink' => $job->ExternalLink
);
}
$rand_job = array_rand($item, 4);
$i=0;
echo '<ul>';
while($i<=3) {
echo '<li>';
echo $item[$i]['OrganizationName'];
echo '</li>';
$i++;
}
echo '</ul>'
What do I need to do differently? Thanks!
You have to use simplexml_load_file($url); or similar.
$url = 'https://rss.myinterfase.com/rss/oxford_RSS_Jobs_xml.xml';
$xml = simplexml_load_file($url);
foreach($xml->row as $job) { // be sure about $xml->row. If it's full path to this elements
//..... your code
}

How to fetch content:encoded tags information from rss

Am fetching blog from wordpress to my website using magpierss-0.72 rss parser now i want to fetch image from my blog, the image in tag like
<content:encoded><img src="path" /></content:encoded>
my code what i have tried is
require_once('rss_fetch.inc');
$rss = fetch_rss($url);
foreach ($rss->items as $i => $item ) {
$title = strtoupper ($item['title']);
$url = $item['link'];
$date = $item['pubdate'];
$desc = $item['description'];
$content = $item['content:encoded'];
echo $title."<br />";
echo $url."<br />";
echo $date."<br />";
echo $desc."<br />";
echo $content."<br />";
}
But the details in content:encode tag is not fetching. Can any one help me Please
Thank you in advance
It should be parsed into the $item['content']['encoded'] field if your feed is an Atom feed or under $item['atom_content'] if your feed is an RSS feed.
For reference see the rss_parse.php, MagpieRSS::parse method.

Inserting multiple rss feeds into MYSQL with simplexml

Below is my code to parse multiple rss feeds into a mysql db.
I do something wrong in the foreach part I think, since there is no output.
The db however, gets filled. When using 1 feed, the script works fine.
Anybody sees what I do wrong? Many thanks in advance :)
$feeds = ('https://www.ictu.nl/rss.xml', 'http://www.vng.nl/smartsite.dws?id=97817');
$xml = simplexml_load_file($feeds);
foreach($xml->channel->item as $item)
{
$date_format = "j-n-Y"; // 7-7-2008
echo date($date_format,strtotime($item->pubDate));
echo ''.$item->title.'';
echo '<div>' . $item->description . '</div>';
mysql_query("INSERT INTO rss_feeds (id, title, description, link, pubdate)
VALUES (
'',
'".mysql_real_escape_string($item->title)."',
'".mysql_real_escape_string($item->description=htmlspecialchars(trim($item->description)))."',
'".mysql_real_escape_string($item->link)."',
'".mysql_real_escape_string($item->pubdate)."')");
}
Try this:
<?php
$feeds = array('https://www.ictu.nl/rss.xml', 'http://www.vng.nl/smartsite.dws?id=97817');
foreach( $feeds as $feed ) {
$xml = simplexml_load_file($feed);
foreach($xml->channel->item as $item)
{
$date_format = "j-n-Y"; // 7-7-2008
echo date($date_format,strtotime($item->pubDate));
echo ''.$item->title.'';
echo '<div>' . $item->description . '</div>';
mysql_query("INSERT INTO rss_feeds (id, title, description, link, pubdate)
VALUES (
'',
'".mysql_real_escape_string($item->title)."',
'".mysql_real_escape_string($item->description=htmlspecialchars(trim($item->description)))."',
'".mysql_real_escape_string($item->link)."',
'".mysql_real_escape_string($item->pubdate)."')");
}
}
?>
Hope it helps.

Removing wrapping HTML elements inside a RSS XML node

I have a fetch function that injects rss content into a page for me. This returns an xml which contains the usual RSS elements like title, link, description but the problem is the returned description is a table with two tds which one contains an image the other the text. I am not sure how I can remove the table, img and the tds and be left only with the text using php and not javascript.
Any help is much appreciated.
<?php
require_once('rss_fetch.inc');
$url = 'http://www.domain.com/rss.aspx?typeid=0&imagesize=120&topcount=20';
if ( $url ) {
$rss = fetch_rss( $url );
//echo "Channel: " . $rss->channel['title'] . "<p>";
echo "<ul>";
foreach ($rss->items as $item) {
$href = $item['link'];
$title = $item['title'];
$description = $item['description'];
$pubdate = date('F dS, Y', strtotime($item['pubdate']));
echo "<li><h3>$title<em>$pubdate</em></h3>$description <p><a href='$href' target='_blank'>ادامه مطلب</a></p><br/></li>";
}
echo "</ul>";
}
?>
strip_tags() will do the job..

Categories