尝试在PHP中使用SimplePie库时出现弃用错误
我正在开发新闻应用程序,我需要从尝试在PHP中使用SimplePie库时出现弃用错误,php,rss,simplepie,Php,Rss,Simplepie,我正在开发新闻应用程序,我需要从RSS(真正简单的联合)解析当前新闻 我发现这个库可以轻松解析RSSfeed 首先,我在服务器上直接使用了这个库和我的php代码 <?php // Make sure SimplePie is included. You may need to change this to match the location of autoloader.php // For 1.0-1.2: #require_once('../simplepie.inc'); //
RSS(真正简单的联合)
解析当前新闻
我发现这个库可以轻松解析RSS
feed
首先,我在服务器上直接使用了这个库和我的php代码
<?php
// Make sure SimplePie is included. You may need to change this to match the location of autoloader.php
// For 1.0-1.2:
#require_once('../simplepie.inc');
// For 1.3+:
require_once('./php/autoloader.php');
// We'll process this feed with all of the default options.
$feed = new SimplePie("https://news.google.com/news/feeds?pz=1&cf=all&ned=us&hl=en&topic=h&num=3&output=rss");
// Set which feed to process.
// Run SimplePie.
$feed->init();
// This makes sure that the content is sent to the browser as text/html and the UTF-8 character set (since we didn't change it).
$feed->handle_content_type();
foreach ($feed->get_items() as $item):
?>
<div class="item">
<h2><a href="<?php echo $item->get_permalink(); ?>"><?php echo $item->get_title(); ?></a></h2>
<p><?php echo $item->get_description(); ?></p>
<p><small>Posted on <?php echo $item->get_date('j F Y | g:i a'); ?></small></p>
</div>
<?php
endforeach;
?>
我认为这是因为PHP版本,但我不认为我能做什么
请帮忙
提前感谢。错误显示:
不推荐使用:不再允许向构造函数传递参数
支持。请使用set\u feed\u url()、set\u cache\u location()和
直接设置缓存位置()
错误很明显。你不应该这样做:
$feed = new SimplePie("https://news.google.com/news/feeds?pz=1&cf=all&ned=us&hl=en&topic=h&num=3&output=rss");
$feed = new SimplePie();
(当您使用new
操作符创建类实例时,构造函数是自动调用的函数,这是您的困惑所在。)该函数明确表示:
以前,可以将提要URL与缓存一起传递
选项直接输入到构造函数中。从1.3开始,此选项已被删除
因为它引起了很多混乱
相反,您必须这样做:
$feed = new SimplePie("https://news.google.com/news/feeds?pz=1&cf=all&ned=us&hl=en&topic=h&num=3&output=rss");
$feed = new SimplePie();
。。。并使用适当的方法提供参数。顾名思义,可以用来提供提要的URL。您是如何解决这个问题的?我的印象是您没有阅读错误消息,因为它解释了问题所在以及您必须做什么。不管怎样,我已经添加了一个答案,试图详细解释它。