问题
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