Use simplepie with composer

倖福魔咒の 提交于 2020-06-01 04:47:06

问题


I am using PHP 7.3.5 and Composer version 1.8.5 and would like to use "simplepie/simplepie": "^1.5" to get data from an rss feed.

When running the below script I get the error: Fatal error: Uncaught Error: Class 'SimplePie\SimplePie' not found

<?php
require 'vendor/autoload.php';

use SimplePie\SimplePie;

$url = 'https://www.reddit.com/r/worldnews/top.rss?t=day';

$feed = new SimplePie();
$feed->set_feed_url($url);
$feed->init();

How to correctly use simplepie with composer?


回答1:


You can try:

use SimplePie;

This worked in one of my projects.

more complete example (with in composer.json : "simplepie/simplepie": "^1.5"):

use SimplePie;

class RssParser{
    public $url;
    private $feed;
    private $message; // personal class for holding error...

    function __construct($url){

        if (!empty($this->url)) {
            $this->feed = new SimplePie();
            $this->feed->set_feed_url($this->url);
            $this->feed->set_cache_duration(600);

            if ($this->feed->init() === false) {
                $this->message->error('rss-parser NO SimplePie init: ' . $this->feed->error());
            } else {
                $this->feed->handle_content_type();
            }
        } else {
            $this->message->error('rss-parser: no url');
        }
    }

    ...

I'm not a Composer/autoloading specialist, but since Simplepie uses its own autoloader, that may explain this writing.



来源:https://stackoverflow.com/questions/58698483/use-simplepie-with-composer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!