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.
Related
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
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
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>
I need your help once again!
I need to read this xml file... but the problem is that it's not working!
This is the XML
<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<item>
<title>Video</title>
<media:content url="http://videourl.com/etc/" type="video/x-flv" duration="5128"/>
</item>
</channel>
</rss>
And this is my code:
<?php
$xml=simplexml_load_file("http://videourl.com/etc/");
echo $xml->getName() . "<media:content url=";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "";
}
?>
And it's not working! It's not working because nothing gets echoed, or printed! Does anyone spot the error?
<?php
$xml = '<?xml version="1.0" encoding="UTF-8" ?>
<rss>
<channel>
<item>
<title><![CDATA[Tom & Jerry]]></title>
</item>
</channel>
</rss>';
$xml = simplexml_load_string($xml);
// echo does the casting for you
echo $xml->channel->item->title;
// but vardump (or print_r) not!
var_dump($xml->channel->item->title);
// so cast the SimpleXML Element to 'string' solve this issue
var_dump((string) $xml->channel->item->title);
?>
Again i edit my code now try this
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 ...