【问题标题】:Deprecation error when trying to use SimplePie library in PHP尝试在 PHP 中使用 SimplePie 库时出现弃用错误
【发布时间】:2013-08-09 11:17:32
【问题描述】:

我正在开发新闻应用程序,我需要解析来自RSS (Really Simple Syndication) 的当前新闻。

我发现SimplePie 库可以轻松解析RSS 提要。

首先,我通过php code 在服务器上直接使用了这个库。

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

但是我在我的电脑上运行这个文件,我得到了以下错误:

Deprecated: Passing parameters to the constructor is no longer supported. Please use set_feed_url(), set_cache_location(), and set_cache_location() directly. in C:\xampp\htdocs\apps\liveibl\php\library\SimplePie.php on line 640

我认为这是因为 PHP 版本而发生的,但我不认为我能做什么。

请帮忙...

提前致谢。

【问题讨论】:

  • 你是怎么解决这个问题的?
  • 我的印象是您没有阅读错误消息,因为它说明了问题所在以及您必须做什么。无论如何,我已经添加了一个试图详细解释的答案。

标签: php rss simplepie


【解决方案1】:

错误提示:

已弃用:不再向构造函数传递参数 支持的。请使用 set_feed_url()、set_cache_location() 和 直接set_cache_location()

错误很明显。你不应该这样做:

$feed = new SimplePie("https://news.google.com/news/feeds?pz=1&cf=all&ned=us&hl=en&topic=h&num=3&output=rss");

构造函数是在您使用new 运算符创建类实例时自动调用的函数,这让您感到困惑。)documentation 明确表示:

以前,可以将提要 URL 与缓存一起传递 选项直接进入构造函数。从 1.3 开始,这已被删除 因为它引起了很多混乱。

相反,您必须这样做:

$feed = new SimplePie();

...并使用适当的方法来提供参数。顾名思义,set_feed_url() 可用于提供提要的 URL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 2020-08-10
    • 1970-01-01
    • 2012-02-26
    • 2020-06-24
    相关资源
    最近更新 更多