Rss feed not displaying as feed in browsers - php

<?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?

Related

How do I get the child nodes of this RSS feed?

How can I get the contest logo and start date from this RSS feed? I can get the dc:modified child for example but always get a blank for anything from dc:dataset.
My code:
$feed_url = 'https://www.website.com/?call_custom_simple_rss=1&csrp_post_type=contest&csrp_posts_per_page=2&csrp_show_meta=1';
$feed = file_get_contents($feed_url);
$rss = simplexml_load_string($feed);
foreach($rss->channel->item as $entry) {
echo $entry->children("dc", true)->modified . "<br>";
echo $entry->children("dc", true)->dataset->contest_logo . "<br>";
echo $entry->children("dc", true)->dataset->start_date . "<br>";
}
The RSS feed:
<rss 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/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:wp="http://wordpress.org/export/1.2/" xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/" version="2.0">
<channel>
<title>RSS Title</title>
<description>A website</description>
<lastBuildDate>Wed, 17 Feb 2021 15:03:03 +0000</lastBuildDate>
<item>
<title>
<![CDATA[ Photography Awards ]]>
</title>
<link>
<![CDATA[ /contests/photography-awards/ ]]>
</link>
<pubDate>Mon, 11 Jan 2021 13:52:27 -0600</pubDate>
<dc:identifier>619116</dc:identifier>
<dc:modified>2021-02-09 07:50:10</dc:modified>
<dc:created unix="1610373147">2021-01-11 13:52:27</dc:created>
<dc:dataset>
<contest_logo>
<![CDATA[ 619130 ]]>
</contest_logo>
<start_date>
<![CDATA[ 20210110 ]]>
</start_date>
</dc:dataset>
</item>
</channel>
</rss>
The contest_logo and start_date are in the empty namespace. You have to switch back. Additionally it is not good to reply on namespace prefixes defined in the document. Use the namespace URI (for example defined as mapping array in your code).
$rss = simplexml_load_string($feed);
$xmlns = [
'dc' => 'http://purl.org/dc/elements/1.1/'
];
foreach($rss->channel->item as $entry) {
echo $entry->children($xmlns['dc'])->modified . "<br>";
echo $entry->children($xmlns['dc'])->dataset->children('')->contest_logo . "<br>";
echo $entry->children($xmlns['dc'])->dataset->children('')->start_date . "<br>";
}
Output:
2021-02-09 07:50:10<br>
619130
<br>
20210110
<br>
In DOM you would register an alias on the Xpath processor and use it in the expressions. Here is a demo:
$document = new DOMDocument();
$document->loadXML($feed);
$xpath = new DOMXpath($document);
$xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/');
foreach ($xpath->evaluate('/rss/channel/item') as $entry) {
echo $xpath->evaluate('string(dc:modified)', $entry). "<br>";
echo $xpath->evaluate('string(dc:dataset/contest_logo)', $entry). "<br>";
echo $xpath->evaluate('string(dc:dataset/start_date)', $entry). "<br>";
}
Another alternative - use xpath:
echo $rss->xpath('//dc:dataset/contest_logo')[0] . "\r\n";
echo $rss->xpath('//dc:modified')[0] . "\r\n";
echo $rss->xpath('//start_date')[0] . "\r\n";
Output:
619130
2021-02-09 07:50:10
20210110

Get XML Attributes using PHP

I want to get the URL of the image in . The XML document tree is as follow:
<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<title>
<![CDATA[ The Star Online Business Highlights ]]>
</title>
<link>/TheStar/Website</link>
<description>...</description>
<image>...</image>
<language>en</language>
<item>
<guid isPermaLink="false">{F88B27DD-24FB-4807-941F-070D772B7586}</guid>
<link>
http://www.thestar.com.my/business/business-news/2017/10/24/top-glove-says-not-buying-adventa-nor-supermax/
</link>
<title>
<![CDATA[ Top Glove says not buying Adventa nor Supermax ]]>
</title>
<description>
<![CDATA[KUALA LUMPUR: Top Glove, which has allocated about RM1bil to expand via mergers, has denied news reports the target companies are Adventa Bhd and Supermax Corporation Bhd.]]>
</description>
<pubDate>Tue, 24 Oct 2017 13:17:18 +08:00</pubDate>
<enclosure url="http://www.thestar.com.my/~/media/online/2017/08/22/03/58/hartalega-glove3.ashx?crop=1&w=0&h=0&" length="" type="image/jpeg"/>
<media:content url="http://www.thestar.com.my/~/media/online/2017/08/22/03/58/hartalega-glove3.ashx?crop=1&w=0&h=0&" type="image/jpeg">
<media:description>
<![CDATA[ ]]>
</media:description>
</media:content>
<section>
<![CDATA[ Business ]]>
</section>
</item>
<item>...</item>
<item>...</item>
<item>...</item>
</channel>
As there is multiple item and I want to make it a loop, I tried:
foreach($xml->channel->item as $news) {
$media = $news->media->children('http://search.yahoo.com/mrss/');
echo ($media->content);
}
and also
foreach($xml->channel->item as $news) {
$media = $news->children('http://search.yahoo.com/mrss/');
echo ($media->content);
}
but both are seems failed. What is the right method?
The $media variable is of type SimpleXMLElement.
What you could do is loop your $media variable in a foreach and then get your url from the attributes.
For example (using simplexml_load_string with additional Libxml parameters to load your example xml:
$source = <<<SOURCE
//Your example xml here
SOURCE;
$xml = simplexml_load_string($source, "SimpleXMLElement", LIBXML_NOERROR|LIBXML_ERR_NONE|LIBXML_ERR_FATAL);
foreach($xml->channel->item as $news) {
$media = $news->children('http://search.yahoo.com/mrss/');
foreach($media as $child) {
echo $child->attributes()->url;
}
}
Will result in:
http://www.thestar.com.my/~/media/online/2017/08/22/03/58/hartalega-glove3.ashx?crop=1=0=0
$xml = new SimpleXMLElement($xml, LIBXML_NOERROR|LIBXML_ERR_NONE|LIBXML_ERR_FATAL);
foreach ($xml->xpath("//media:content") as $node)
{
var_dump ((string) $node["url"]);
}

SimplePie on Azure not parsing https feeds

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

RSS Feed using Codeigniter

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

php auto rss feed filemtime error stat failed

I have a php code that automatically generates an RSS feed XML file. Now I have my pages which are in a folder. I read them out with simple php to generate title, links, description... that all goes fine and the code works great. But when I try to get the last timestamp of the file itself I get errors. I really can't figure out why the code gives errors.
<?php
$rssfeed = "<?xml version='1.0' encoding='ISO-8859-1'?>
<rss version='2.0'>
<channel>
<title>My RSS feed</title>
<link>http://" . $_SERVER['HTTP_HOST'] . "/</link>
<description>This is an example RSS feed</description>
<language>en-us</language>
<copyright>Copyright (C) 2009 mywebsite.com</copyright>
";
$links = scandir('../pages/');
$links = array_diff($links, array('.', '..', 'subpages', 'protected'));
foreach($links as $link){
$descr = file_get_contents('../description/' . $link);
$descr = str_replace(array('\\'), array(''), $descr);
$pub = date ('F d Y H:i:s.', filemtime($link));
$rssfeed .= "<item>
<title>".$link."</title>
<description>".$descr."</description>
<link>http://" . $_SERVER['HTTP_HOST'] . "/index.php?p=".$link."</link>
<pubDate>".$pub."</pubDate>
</item>";
}
$rssfeed .= "</channel></rss>";
file_put_contents("../RSSfeed.xml", $rssfeed, LOCK_EX);
?>
this is the error I get:
Warning: filemtime(): stat failed for 01Home in C:\Program Files\EasyPHP-DevServer-13.1VC9\data\localweb\admin\rss.php on line 35
(localhost on a easyphp server for testing)

Categories