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
}
}
Related
Below is my PHP code and XML file, I have been trying with so many different echos to loop through my images in my XML file to display each image to correct product but can only display the first image to all three products.
XML code:
<my_products>
<product>
<id>1</id>
<image> csuT.jpg</image>
<name>Champion T-Shirt</name>
<price>18.00</price>
<description>
Get the perfect look to let everyone know you are a stylish fan!
</description>
</product>
<product>
<id>2</id>
<image> webBook.jpg</image>
<name>C# Programming: Analysis to Program Design</name>
<price>192.00</price>
<description>
Your hands-on guide to Microsoft Visual C# fundamentals with Visual Studio 2017
</description>
</product>
<product>
<id>3</id>
<image> calcPic.jpg</image>
<name>Calculator TI-BAII Plus 10DIG/24CASH</name>
<price>39.00</price>
<description>
Performs common math as well as various financial functions
</description>
</product>
</my_products>
PHP code:
$xml = simplexml_load_file($file);
$script_images="";
$script_products="";
$script_product_prices="";
//Loop through the products defined in the products.xml file
foreach ($xml->product as $r)
{
$script_images.="products[".($r->id)."]=\"".($r->image)."\";\n";
$script_products.="products[".($r->id)."]=\"".($r->name)."\";\n";
$script_product_prices.="product_prices[".($r->id)."]=\"".($r->price)."\";\n";
?>
<div>
<p class="lead">
<h3 class="pull-right no-top-margin"><?php echo $currency_symbol;?><?php echo $r->price;?></h3>
<h3><?php echo "<image src='csuT.jpg' 'calcPic.jpg' 'webBook.jpg'/>";?></h3>
</p>
<h3><?php echo $r->name;?></h3>
</p>
<p>
<?php echo $r->description;?>
</p>
<br/>
<?php
//If there is details link set for the product, show a Details button
if(trim($r->details_link)!="")
{
?>
<a target="_blank" href="http://<?php echo str_replace("http://","",trim($r->details_link));?>"</a>
<?php
}
?>
<a class="btn btn-xs btn-info" href="javascript:AddToCart(<?php echo $r->id;?>)">Add to Cart</a>
</div>
<hr/>
<?php
}
?>
<script>
var currency_symbol="<?php echo $currency_symbol;?>";
var products=Array();
<?php echo $script_images;?>
var product_images=Array();
<?php echo $script_products;?>
var product_prices=Array();
<?php echo $script_product_prices;?>
</script>
You just need to extract the image from the XML (the same way as done for the Javascript) and I've added a trim() to remove any spaces round the field...
<h3><?php $image = trim($r->image);
echo "<image src='$image'/>";?></h3>
Also add trim to...
$script_products.="products[".($r->id)."]=\"".($r->name)."\";\n";
What is this? :
<h3><?php echo "<image src='csuT.jpg' 'calcPic.jpg' 'webBook.jpg'/>";?></h3>
your src attribute is static and you must change it
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> Content</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.
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>
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
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!