Building an RSS Feed in Code Igniter - php

I want building an RSS Feed but my output not true, how can fix it?
i use this tutorial:http://www.derekallard.com/blog/post/building-an-rss-feed-in-code-igniter/
Please see my full code in following:
CI_Controller:
function rss_feed($limit = NULL)
{
$data['encoding'] = 'utf-8';
$data['feed_name'] = 'neginph.com';
$data['feed_url'] = 'http://www.neginph.com';
$data['page_description'] = 'Mono-calcium phosphate and calcium phosphate producer in the Persian month Dey';
$data['page_language'] = 'en-en';
$data['creator_email'] = 'Negin phosphate North';
$data['posts'] = $this->db->order_by("id", "asc")->get_where('rss_feed', array('id' => 1));;
header("Content-Type: application/rss+xml");
$this->load->view('rss_feed', $data);
}
View:
<?php
echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
?>
<rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:admin="http://webns.net/mvcb/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title><?php echo $feed_name; ?></title>
<link><?php echo $feed_url; ?></link>
<description><?php echo $page_description; ?></description>
<dc:language><?php echo $page_language; ?></dc:language>
<dc:creator><?php echo $creator_email; ?></dc:creator>
<dc:rights>Copyright <?php echo gmdate("Y", time()); ?></dc:rights>
<admin:generatorAgent rdf:resource="http://www.codeigniter.com/" />
<?php foreach($posts->result() as $entry): ?>
<item>
<title><?php echo xml_convert($entry->post_title); ?></title>
<link><?php echo site_url('blog/post/' . $entry->url_title) ?></link>
<guid><?php echo site_url('blog/post/' . $entry->url_title) ?></guid>
<description><![CDATA[
<?= str_replace('/img/post_resources/', base_url() . 'img/post_resources/', $entry->post_body); ?>
]]></description>
<pubDate><?php echo date ('r', $entry->post_date);?></pubDate>
</item>
<?php endforeach; ?>
</channel></rss>
This is my output in url, why This is shown output code as this? : http://www.neginph.com/site/rss_feed

Respect the MVC, use "$this->db->order_by("id", "asc")->get_where ..." in a CI_Model, example:
your CI_Controller:
function rss_feed()
{
$data['encoding'] = 'utf-8'; // the encoding
$data['feed_name'] = 'MyWebsite.com'; // your website
$data['feed_url'] = 'http://www.MyWebsite.com/feed'; // the url to your feed
$data['page_description'] = 'What my site is about comes here'; // some description
$data['page_language'] = 'en-en'; // the language
$data['creator_email'] = 'mail#me.com'; // your email
$data['posts'] = $this->feed_model->getRecentPosts();
header("Content-Type: application/rss+xml"); // important!
$this->load->view('rss', $data);
}
in you CI_Model:
class Feed_Model extends CI_Model {
// get all postings
function getRecentPosts()
{
$this->db->order_by('id', 'asc');
$this->db->limit(10);
return $this->db->get('posts');
}
}
And you feed:
<?php
echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
?>
<rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:admin="http://webns.net/mvcb/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title><?php echo $feed_name; ?> </title>
<link><?php echo $feed_url; ?> </link>
<description><?php echo $page_description; ?></description>
<dc:language><?php echo $page_language; ?></dc:language>
<dc:creator><?php echo $creator_email; ?></dc:creator>
<dc:rights>Copyright <?php echo gmdate("Y", time()); ?></dc:rights>
<admin:generatorAgent rdf:resource="http://www.codeigniter.com/" />
<?php foreach($posts->result() as $entry): ?>
<item>
<title><?php echo xml_convert($entry->title); ?></title>
<link><?php echo site_url('blog/post/' . $entry->id) ?></link>
<guid><?php echo site_url('blog/post/' . $entry->id) ?></guid>
<description><![CDATA[<?php echo character_limiter($entry->text, 200); ?>]]></description>
<pubDate><?php echo date ('r', $entry->date);?></pubDate>
</item>
<?php endforeach; ?>
</admin:generatoragent>
</channel>
</rss>

$entry->post_body isn't defined
Just a small thing as well, but you have two semicolons: $data['posts'] = $this->db->order_by("id", "asc")->get_where('rss_feed', array('id' => 1));; and that is bugging me. Valid yes, but I don't like it!

Related

Remove HTML from PHP String - Wordpress RSS Feed Template

Thanks for all the help; nothing working as of yet, so adding more detail.
I'm using the below function in functions.php to allow me to use my own RSS template, which is working fine.
add_action('init', 'customRSS');
function customRSS(){
add_feed('shows', 'customRSSFunc');
}
function customRSSFunc(){
get_template_part('rss', 'shows');
}
The php template for the feed is:
<?php
header('Content-Type: ' . feed_content_type('rss2') . '; charset=' . get_option('blog_charset'), true);
$more = 1;
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>';
do_action( 'rss_tag_pre', 'rss2' );
?>
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
<?php do_action( 'rss2_ns' );?>>
<channel>
<title><?php wp_title_rss(); ?></title>
<atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
<link><?php bloginfo_rss('url') ?></link>
<description><?php bloginfo_rss("description") ?></description>
<lastBuildDate><?php
$date = get_lastpostmodified( 'GMT' );
echo $date ? mysql2date( 'r', $date, false ) : date( 'r' );
?></lastBuildDate>
<language><?php bloginfo_rss( 'language' ); ?></language>
<sy:updatePeriod><?php
$duration = 'hourly';
echo apply_filters( 'rss_update_period', $duration );
?></sy:updatePeriod>
<sy:updateFrequency><?php
$frequency = '1';
echo apply_filters( 'rss_update_frequency', $frequency );
?></sy:updateFrequency>
<?php do_action( 'rss2_head'); while( have_posts()) : the_post();?>
<item>
<title><?php the_title_rss(); ?></title>
<date><?php echo customevent_get_start_date( null, false, 'd-m-Y' ); ?></date>
<time><?php echo customevent_get_start_date( null, false, 'H:i' ); ?></time>
<price><?php echo customevent_get_cost(); ?></price>
<link><?php the_permalink_rss(); ?></link>
<imageurl><?php the_post_thumbnail_url(); ?></imageurl>
<description>
<?php
$content = the_content();
echo $content ; ?>
</description>
<?php rss_enclosure(); ?>
<?php do_action('rss2_item'); ?>
</item>
<?php endwhile; ?>
</channel>
</rss>
The part I'm having issues with is the description (the_content). The output on the feed is showing p tags and more, as well as in numerous locations. Both of which need to be stripped.
I've tried:
<description>
<?php
$content = the_content();
echo wp_filter_nohtml_kses($content); ?>
</description>
and
<description>
<?php
$content = the_content();
echo strip_tags(html_entity_decode($content)); ?>
</description>
Output in feed:
<description>
<p><strong>It took Michael Portillo little more than 10 years to get a seat in the Commons and then rise in power and esteem to a point where he was a favoured leader of his party and possible future PM. A track record like that suggests a privileged friend of the rich and famous ,but since leaving the house almost a decade ago Michael has endeared himself to many with his obvious respect for solid workmanship as found in our great Victorian Railways and the daily life of ordinary hard working citizens. Listen to his story, told with a “ parliamentary standup ” wit, and then feel free to question him about it. </strong></p>
<p><strong>Full Price – £19.50 </strong></p>
<p><strong>Concession – £18.50 </strong></p>
</description>
use this one.
wp_filter_nohtml_kses( string $data );
check this link.
https://developer.wordpress.org/reference/functions/wp_filter_nohtml_kses/
You can wrap str_replace with strip_tags like this:
<?php
function the_content(){
echo '<p>&nbspContent</p>';
}
$content = the_content();
echo str_replace(' ','',strip_tags($content));
?>
I tested this and it works fine. If this isn't working for you could you please show the content of your function?
EDIT:
I would also recommend you look at html_entity_decode here:
https://www.w3schools.com/php/func_string_html_entity_decode.asp
This is the best way to achieve what you want. My answer above is to provide a direct answer to what you ask but this is the better alternative.

Output items from XML feed when matching to category

I've exported all product items from my shop as an XML-File and parsed these items with PHP in my single.php of my theme. The parse works fine so far.
Now i want to only display products which category name matches with the ones that have been set in the article editor of WP. This is my code:
<?php
// Parse XML Feed
$html = "";
$url = "http://example.com/feed.xml";
$xml = simplexml_load_file($url);
$item = $xml->channel->item;
$categories = get_the_category();
for($i = 0; $i < count($item); $i++){
$title = $item[$i]->title;
$link = $item[$i]->link;
$category = $item[$i]->category;
$img = $item[$i]->description->a->img["src"];
// This approach breaks my site. A loop in another loop seems not to be that good
// foreach ($categories as $i => $cat) {
// $catName = $cat->name;
// if (preg_match("/\b$catName\b/", $category)) {
// $html .= "
// <a href='$link'><h3>$title</h3>
// <img src='$img'>
// <p>$category</p></a>
// <hr>";
// }
// }
if (preg_match("/\bCATEGORY_NAME\b/", $category)) {
$html .= "
<a href='$link'><h3>$title</h3>
<img src='$img'>
<p>$category</p></a>
<hr>";
}
}
echo $html;
?>
If i change CATEGORY_NAME to an category name which exits, then the output works like i want. The other approach with that loop breaks my site with an "Fatal error: Allowed memory size of *** bytes exhausted…".
Would love to hear some tips
EDIT: Here is some example code
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="www.example.com/export.xml" rel="self" type="application/rss+xml" />
<title>www.example.com</title>
<description>desc</description>
<link>www.example.com</link>
<language>1-1</language>
<image>
<url>www.example.com/logo.gif</url>
<title>test page</title>
<link>http://example.com</link>
</image>
<item>
<title>Example item</title>
<guid>www.example.com/item</guid>
<link>www.example.com/item</link>
<description>
<a href="www.example.com/item" style="border:0 none;">
<img src="www.example.com/image.png" align="right" style="padding: 0pt 0pt 12px 12px; float: right;" />
</a>
</description>
<category>Categoryname</category>
<pubDate>Thu, 24 Aug 2017 17:19:17 +0200</pubDate>rn</item>
</channel>
</rss>

CodeIgniter RSS output as a valid feed

I Cannot get the RSS to work as a valid feed. This the rss: http://mimjob.com/news/rss
PHP:
<?php ob_start(); echo'<?xml version="1.0" encoding="utf-8" ?>' . "\n"; ?>
<rss version="2.0"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:admin="http://webns.net/mvcb/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
<title><?php echo $feed_name; ?> </title>
<link><?php echo $feed_url; ?> </link>
<description><?php echo $page_description; ?></description>
<dc:language><?php echo $page_language; ?></dc:language>
<dc:creator><?php echo $creator_email; ?></dc:creator>
<dc:rights>Copyright <?php echo gmdate("Y", time()); ?></dc:rights>
<admin:generatorAgent rdf:resource="http://www.mzksh.com/" />
<?php foreach($news->result() as $n): ?>
<item>
<title><?php echo xml_convert($n->title); ?></title>
<link><?php echo base_url('news/get/' . $n->id) ?></link>
<guid><?php echo base_url('news/get/' . $n->id) ?></guid>
<description><![CDATA[<?php echo character_limiter($n->text, 200); ?>]]></description>
<pubDate><?php echo $n->date;?></pubDate>
</item>
<?php endforeach; ?>
</admin:generatoragent>
</channel>
</rss>
<?php
$output = ob_get_contents();
ob_end_clean();
echo $output;
?>
I've added ob_start to remove empty spaces but still can't get it to work. I've also checked if there are any white spaces before <?php.
I'm using codeigniter framework.
That's because CodeIgniter handles all outputs via its Output Class.
First remove the ob_start section from the view file, then change the current Content-type and output by this way:
class Home extends CI_Controller
{
public function rss()
{
$data = $this->load->view('your_rss_view_file', '', TRUE);
$this->output
->set_content_type('application/rss+xml') // This is the standard MIME type
->set_output($data); // set the output
}
}
You simply need to put the xml headers before you echo something to the browser. Example:
class homepage extends CI_Controller{
function index(){
header("Content-type: text/xml; charset=utf-8");
$this->load->view('my_view_file'); // this is your code from the example
}
}

Custom XML RSs feed

for the past 6 weeks I have been developing a custom social network in php and mysqli. Anyway today I decided to create a feed for the blogging system. I have checked my script with the w3 feed validation service and it works fine!
The problem is that none of the feeds are showing up on the page, here is my script.....>
<?php include"php_includes/db_conx.php"; ?>
<?php header('Content-type: text/xml'); ?>
<?php echo '<?xml version="1.0" encoding="iso-8859-1"?>';
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Birdbooks Blog Feed</title>
<description>Birdbook is a Social Network with Birders in mind</description>
<link>http://www.birdbook.org.uk/</link> <copyright> copyright 2013 Birdbook Social Network</copyright>
<atom:link href="http://www.birdbook.org.uk/feed/" rel="self" type="application/rss+xml" />
<?php $query = "SELECT * FROM 'blog' ORDER BY 'delimiter' DESC LIMIT 0,15";
$results = $db_conx->query($query);
$number = $results->num_rows;
for ($i = 1; $i <= $number; $i++) {
$row = $results->fetch_assoc();
$title = $row['blogtitle'];
$description = $row['description'];
$link = $row['linklabel'];
$date = date("r", $row['lastmodified']);
?>
<item>
<title><?php echo $title; ?></title>
<description><?php echo $description; ?></description>
<link><?php echo $link; ?></link>
<pubDate><?php echo $date; ?></pubDate>
<guid><?php echo $link; ?></guid>
</item>
<?php } ?>
</channel>
</rss><?php $db->close(); ?>
Anyone got any Ideas?
Many thanks.. Phillip Dews

Parse XML to a nested list with PHP

I have an XML file which contains family tree data in a nested structure, and I'm wanting to parse it into a nested list.
I have the following code
<?php
$doc = new DOMDocument();
$doc->load('armstrong.xml');
echo $doc->saveXML();
?>
Which loads in the following XML file and prints it as-is
<?xml version="1.0" encoding="UTF-8"?>
<indi>
<id>id1</id>
<fn>Matt</fn>
<bday>1919</bday>
<dday>2000</dday>
<spouse>Evelyn Ross</spouse>
<family>
<indi>
<id>id2</id>
<fn>Jane</fn>
<bday></bday>
<dday></dday>
<spouse></spouse>
<family>
</family>
</indi>
<indi>
<id>id3</id>
<fn>Jason</fn>
<bday></bday>
<dday></dday>
<spouse></spouse>
<family>
</family>
</indi>
<indi>
<id>id4</id>
<fn>Samuel</fn>
<bday></bday>
<dday></dday>
<spouse></spouse>
<family>
<indi>
<id>id5</id>
<fn>John</fn>
<bday></bday>
<dday></dday>
<spouse></spouse>
<family>
</family>
</indi>
<indi>
<id>id6</id>
<fn>John</fn>
<bday></bday>
<dday></dday>
<spouse></spouse>
<family>
</family>
</indi>
</family>
</indi>
</family>
However I want to parse it into the following format:
<ul>
<li>
<span class="vcard person" id="id1">
<span class="edit fn">Matt</span>
<span class="edit bday">1956</span>
<span class="edit dday"></span>
<span class="edit spouse">Eunace Fulton</span>
</span>
<ul> ... List of Family ... </ul>
</li>
</ul>
I'm pretty new to php, so if this is an incredibly simple problem I apologise! Would really appreciate any ideas.
EDIT
I'm now using the following recursive loop but still having problems
$doc = new DOMDocument();
$doc->load('armstrong.xml');
function outputIndi($indi) {
$i = new DOMDocument();
$i = $indi;
echo '<li>';
echo '<span class="edit fn">' . $indi->getElementsByTagName("fn") . '</span>'; // name not a real attribute, must access through DOM
echo '<span class="edit bday">' . $indi->getElementsByTagName("bday") . '</span>'; // ditto
// ...
echo '<ul>';
foreach ($indi->getElementsByTagName("family") as $subIndi) { // again, family not a real attribute
outputIndi($subIndi);
}
echo '</ul>';
echo '</li>';
}
outputIndi($doc->documentRoot);
?>
Here's your code. You'll need to add the rest of the attributes (dday, spouse)
RECURSION!
function outputIndi($indi) {
echo '<li>';
$id = $indi->getElementsByTagName('id')->item(0)->nodeValue;
echo '<span class="vcard person" id="' . $id . '">';
$fn = $indi->getElementsByTagName('fn')->item(0)->nodeValue;
$bday = $indi->getElementsByTagName('bday')->item(0)->nodeValue;
echo '<span class="edit fn">' . $fn . '</span>';
echo '<span class="edit bday">' . $bday . '</span>';
// ...
echo '<ul>';
$family = $indi->getElementsByTagName('family')->item(0)->childNodes;
foreach ($family as $subIndi) {
outputIndi($subIndi);
}
echo '</ul>';
echo '</span>';
echo '</li>';
}
$doc = new DOMDocument();
$doc->load('armstrong.xml');
outputIndi($doc->documentElement);
You see, it outputs all information about an "indi", loops through each child of <family>, and calls itself on that. Does that make sense?

Categories