I create an RSS Feed using Codeigniter following tutorials from here
but my problem is the feed content is not displaying on the page but available if you browse the html source.
here is my controller code:
public function __construct()
{
parent::__construct();
$this->load->model('articles_model');
$this->load->helper('xml');
$this->load->helper('text');
}
public function index()
{
$data['feed_name'] = 'MyDebut.ph';
$data['encoding'] = 'utf-8';
$data['feed_url'] = 'http://www.mydebut.com/feeds';
$data['page_description'] = 'Everything for turning 18.';
$data['page_language'] = 'en-en';
$data['creator_email'] = 'mydebutph#gmail.com';
$data['posts'] = $this->articles_model->getArticlesPaginated(0,30);
header("Content-type: text/xml; charset=utf-8");
$this->load->view('rss', $data);
}
my view code:
<?php echo '<?xml version="1.0" encoding="' . $encoding . '"?>' . "\n"; ?>
<rss version="2.0">
<channel>
<title><?php echo $feed_name; ?></title>
<description><?php echo $page_description; ?></description>
<link><?php echo $feed_url; ?></link>
<?php foreach ($posts as $post): ?>
<item>
<title><?php echo xml_convert($post['art_title']); ?></title>
<link><?php echo base_url().'blogs/'.$post['sec_slug'].'/'.$post['cat_slug'].'/'.$post['art_slug'];?>
<description><?php echo character_limiter($post['art_sub'], 300); ?></description>
</item>
<?php endforeach; ?>
</channel>
</rss>
View Buffer
Make sure you set the buffer(view as string) by passing true as the third parameter
$view = $this->load->view('rss', array('data'=>$data), true);
Ouput Class
You can use the output class to send the buffer(not the view itself) directly to the browser
$this->output->set_content_type('application/rss+xml')->set_output(file_get_contents($view));
Use Proper Meta attributes
<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/">
Link to Xml Document
Add this to the head of the document. (only on blog page)
<link rel="alternate" type="application/rss=xml" title="Blog Feed" href="<?php echo site_url('rss'); ?> "/>
I don't know what is in your Model code but as far I can see in your controller you aren't getting the result array from the model. Check the 22 line of the rss view...
<?php foreach($posts->result() as $post): ?>
Also check this documentation, maybe could help you
Related
<?php
include 'classes/db.connect.php';
if (isset($_GET['user'])) {
if($blog->blog_check($_GET['user'])) {
$username = strip_tags($_GET['user']);
} else {
http_response_code(404); include('html/blogs/404.html'); die;
}
} else {
http_response_code(404); include('html/blogs/404.html'); die;
}
$current_page = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$url = $blog->get_blogger_branding($username, 'gravurl');
$img = 'assets/blogs/images/'.$_GET['user'].'.png';
file_put_contents($img, file_get_contents($url));
$fbimg = 'https://afpayday.com/assets/blogs/images/'.$_GET['user'].'.png';
header('Content-Type: application/rss+xml');
echo '<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>'.ucfirst($blog->get_blogger_branding($username, "fname")).'\'s Payday Blog</title>
<description>Learn about affiliate marketing and affiliate product reviews.</description>
<link>https://afpayday.com/'.$username.'</link>
<copyright>Copyright 2020 Affiliate Payday</copyright>
<language>en-us</language>
<managingEditor>admin#afpayday.com (Bruce Bates)</managingEditor>
<pubDate>Sun, 23 Feb 2020 09:50:44 -0700</pubDate>
<webMaster>admin#afpayday.com (Bruce Bates)</webMaster>
<atom:link href="https://afpayday.com/'.$username.'/rss/" rel="self" type="application/rss+xml" />
<image>
<url>'.$fbimg.'</url>
<title>'.ucfirst($blog->get_blogger_branding($username, "fname")).'\'s Payday Blog</title>
<link>https://afpayday.com/'.$username.'</link>
<description>'.ucfirst($blog->get_blogger_branding($username, "fname")).'\'s Photo</description>
<width>144</width>
<height>144</height>
</image>
<item>
<title>Test</title>
<guid>https://afpayday.com/'.$username.'</guid>
</item>
</channel>
</rss>
';
?>
According to the w3 validator my feed is correctly coded. https://validator.w3.org/feed/check.cgi?url=https%3A%2F%2Fafpayday.com%2Fviraladmin%2Frss%2F
However browsers are not treating it as I would expect.
Firefox tries to download the feed as a file.
Chrome, Opera, and Edge show the feed as code instead of displaying it as a document tree which is what I normally see when viewing an RSS feed.
Only Internet Explorer seems to be handling it properly and showing the "would you like to subscribe to this feed" option.
The feed is here: https://afpayday.com/viraladmin/rss/
I would expect it to look more like https://www.feedforall.com/sample.xml in a browser
What am I doing wrong?
I'm using a compiled version of SimplePie 1.4.2 (the last tagged version on GitHub) to aggregate some rss/atom feeds (code below if needed).
It works well on a couple of linux-based web hosts, but when I upload it to Azure app services only the http feeds display correctly, but https don't.
Why it happens? No specific settings set on web app, using PHP 5.6 in both environments. No differences accessing azure web app through http or https.
Thanks everybody!
<?php
date_default_timezone_set('Europe/Rome');
set_time_limit(0);
header('Content-Type: application/rss+xml; charset=UTF-8');
require_once('SimplePie.compiled.php');
[...]
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title><?php echo $feedtitle; ?></title>
<atom:link href="<?php echo $feedlink; ?>" rel="self" type="application/rss+xml" />
<link><?php echo $feedhome; ?></link>
<description><?php echo $feeddesc; ?></description>
<?php
$feed = new SimplePie();
$feed->set_feed_url($feeds);
$feed->force_feed(true);
$feed->init();
$feed->handle_content_type();
foreach($feed->get_items() as $item) {
?>
<item>
<title><?php echo $item->get_title(); ?></title>
<link><?php echo $item->get_permalink(); ?></link>
<guid><?php echo $item->get_permalink(); ?></guid>
<pubDate><?php echo $item->get_date('D, d M Y H:i:s T'); ?></pubDate>
<dc:creator><?php if ($author = $item->get_author()) { echo $author->get_name()." at "; }; ?><?php if ($feed_title = $item->get_feed()->get_title()) {echo $feed_title;}?></dc:creator>
<description><![CDATA[<?php echo $item->get_content(); ?>]]></description>
</item>
<?
};
?>
</channel>
</rss>
It dosen't work for 'https' urls because the SimplePie leverages cURL to make http requests, and for https requests, the cURL requires verify the host or peer certificate.
You can try the following code snippet to bypass the verification.
$simplePie = new SimplePie();
$simplePie->set_curl_options(
array(
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false
)
);
Here is the similar scenario at https://github.com/simplepie/simplepie/pull/407
I am going bald of this error! I have code that work and code that doesn't work. Don't know what the issue is.
The error message is:
PHP Fatal error: Call to a member function find() on a non-object
look towards the bottom of my my non-working code.
The lines of code in question are:
$post_link = $item->get_link();
$htmlDOM->load_file($post_link);
$image = $htmlDOM->find('img', 0);
if ($image->src) {
echo '<media:thumbnail url="' . $image->src . '" width="320" />';
}
Working code:
<?php
// Create DOM from URL or file
include_once('simple_html_dom.php');
include_once('functions.inc');
$html = new simple_html_dom();
$html->load_file('http://themaroontiger.com/an-advantageous-affair/');
$image = $html->find('img', 0);
if ($image->src) {
echo '<media:thumbnail url="' . $image->src . '" width="320" />';
}
exit;
?>
None Working Code:
<?php
header("Content-Type: application/rss+xml; charset=UTF-8");
echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>Maroon Tiger Feed</title>
<link>http://www.webmaster-source.com</link>
<description>
Maroon Tiger Feed
</description>
<language>en-us</language>
<?
include_once('simple_html_dom2.php');
include_once('simplepie.inc');
include_once('functions.inc');
// MAIN BEGIN
$feed = new SimplePie(); // Create a new instance of SimplePie
$htmlDOM = new simple_html_dom();
$feed->set_feed_url(array(
'http://themaroontiger.com/feed/'
));
$feed->enable_cache('false'); //Do we cache
$feed->set_cache_duration(600); //The cache duration
//$feed->enable_xml_dump(isset($_GET['xmldump']) ? true : false);
$success = $feed->init(); // Initialize SimplePie
$feed->handle_content_type(); // Take care of the character encoding
?>
<? if ($success): ?>
<? $itemlimit=0; ?>
<? foreach($feed->get_items() as $item): ?>
<? if ($itemlimit==20) { break; } ?>
<item>
<title><?= decode_entities($item->get_title()); ?> </title>
<link><?= $item->get_permalink(); ?></link>
<dc:creator>
<?
if ($author = $item->get_author())
{
echo $author->get_name();
}
?>
</dc:creator>
<category><? if ($the_category = $item->get_category()) {
echo $the_category->get_label();
} else {
echo 'Facebook Feed';
};?>
</category>
<description><![CDATA[
<? echo $item->get_description(); ?>
]]></description>
<guid isPermaLink="false"><? echo $item->get_id(); ?></guid>
<pubDate><? echo $item->get_date('D, d M Y H:i:s T'); ?></pubDate>
<?
$post_link = $item->get_link();
$htmlDOM->load_file($post_link);
$image = $htmlDOM->find('img', 0);
if ($image->src) {
echo '<media:thumbnail url="' . $image->src . '" width="320" />';
}
?>
</item>
<? $itemlimit++ ?>
<? endforeach; ?>
<? endif; ?>
</channel>
</rss>
It seems you are looping through a feed, maybe some of the links you are trying to read from aren't valid and since you try to read from them without any checking, that's why you might be getting the error message. So after $htmlDOM->load_file($post_link); check if the file was loaded before continuing.
I'm not knowledgeable in PHP (to set the tone). I found some code to grab an external WP blog rss and display recent posts in my custom PHP page (outside the WP blog). I'm trying to get the post thumbnail, but can't figure it out. Been googling for about 2 hours - figured it was time to as directly. Here's the code that is successfully getting the post title and link:
<?php
$rss = new DOMDocument();
$rss->load('http://website.com/blog/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
echo '<p>'.$title.'<br />';
}
?>
What do I add to get the post thumbnail? This is from my own external WP blog, so I can make changes there if needed, but lost on how to accomplish this.
Thanks in advance
Shawn
Appended - here is the RSS feed code from the blog:
<?php
/**
* RSS 0.92 Feed Template for displaying RSS 0.92 Posts feed.
*
* #package WordPress
*/
header('Content-Type: ' . feed_content_type('rss-http') . '; charset=' . get_option('blog_charset'), true);
$more = 1;
echo '<?xml version="1.0" encoding="'.get_option('blog_charset').'"?'.'>'; ?>
<rss version="0.92">
<channel>
<title><?php bloginfo_rss('name'); wp_title_rss(); ?></title>
<link><?php bloginfo_rss('url') ?></link>
<description><?php bloginfo_rss('description') ?></description>
<media:thumbnail url="thumbs/DSC00385.JPG" width="100" height="100"/>
<lastBuildDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_lastpostmodified('GMT'), false); ?></lastBuildDate>
<docs>http://backend.userland.com/rss092</docs>
<language><?php bloginfo_rss( 'language' ); ?></language>
<?php
/**
* Fires at the end of the RSS Feed Header.
*
* #since 2.0.0
*/
do_action( 'rss_head' );
?>
<?php while (have_posts()) : the_post(); ?>
<item>
<title><?php the_title_rss() ?></title>
<description><![CDATA[<?php the_excerpt_rss() ?>]]></description>
<link><?php the_permalink_rss() ?></link>
<?php
/**
* Fires at the end of each RSS feed item.
*
* #since 2.0.0
*/
do_action( 'rss_item' );
?>
</item>
<?php endwhile; ?>
</channel>
</rss>
The following sample plugin lets you access a custom feed url and outputs customized rss contents. In this case, the url is *http:///?feed=custom_user_feed*. The problem is that it is not formatted properly and Google Chrome does not recognize it as a feed. IE and Firefox do on the other hand.
/* Plugin Name: Sample Custom User Feed */
add_action('init', array(new custom_user_feed, "add_feed"));
class custom_user_feed {
protected $feed_query = 'custom_user_feed';
protected $title = 'custom user feed sample';
protected $link = 'http://www.stackoverflow.com';
protected $urls = array(
"http://stackoverflow.com/feeds"
);
protected $description = 'this is a sample to demonstrate a custom user feed.';
protected $language = 'en-us';
protected $itemlimit = 5;
function add_feed() {
add_feed($this->feed_query, array(&$this, "output_rss"));
}
function output_rss() {
$feed = fetch_feed($this->urls);
$feed->set_item_limit($this->itemlimit);
echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>
<rss version="2.0">
<channel>
<title><?php echo $this->title; ?></title>
<link><?php echo $this->link; ?></link>
<description><?php echo $this->description; ?></description>
<language><?php echo $this->language; ?></language>
<?php
foreach($feed->get_items() as $item) {
?>
<item>
<title><?php echo $item->get_title(); ?></title>
<link><?php echo $item->get_permalink(); ?></link>
<description>
<?php echo $item->get_description(); ?>
</description>
</item>
<?php
}
?>
</channel>
</rss>
<?php
}
}
Could anybody find an error in the format? Thanks.
You may need to use header('Content-Type: text/xml'); too ...