Configuration - simplepie/simplepie-ng GitHub Wiki
Logger
SimplePie supports PSR-3 loggers out of the box. After constructing a new instance of a PSR-3 logger, you can pass it to setLogger()
. This instance will be passed along to all newly-constructed child classes so that the entire stack uses the same logger.
The type-hint for the logger is Psr\Log\LoggerInterface
.
Here’s an example Monolog logger that uses the system’s error log.
<?php
use Monolog\Handler\ErrorLogHandler;
use Monolog\Logger;
use Psr\Log\LogLevel;
use SimplePie\SimplePie;
$logger = new Logger('SimplePie');
$logger->pushHandler(new ErrorLogHandler(
ErrorLogHandler::OPERATING_SYSTEM,
LogLevel::DEBUG,
true,
false
));
$simplepie = (new SimplePie())
->setLogger($logger);
Middleware
All of the feed parsing leverages a middleware pattern. Without any registered middleware, nothing gets parsed by default. All of this is managed using a middleware handler stack.
The type-hint for the middleware handler stack is SimplePie\HandlerStackInterface
, and the type-hint for middleware itself is SimplePie\Middleware\MiddlewareInterface
.
<?php
use SimplePie\HandlerStack;
use SimplePie\Middleware\Json\JsonFeed;
use SimplePie\Middleware\Xml\Atom;
use SimplePie\Middleware\Xml\Rss;
use SimplePie\SimplePie;
$simplepie = (new SimplePie())
->setMiddlewareStack(
(new HandlerStack())
->append(new JsonFeed(), 'jsonfeed')
->append(new Atom(), 'atom10')
->append(new Rss(), 'rss20')
);
In this example, and assuming the feed is XML-based, the Atom 1.0 middleware will run first followed by the RSS 2.0 middleware.
-
If it's an RSS 2.0 feed, it is likely that the Atom 1.0 middleware will have limited results while the RSS 2.0 middleware will populate the results. Since Atom can be embedded inside RSS 2.0 feeds (e.g.,
<atom:enabled>
), it is possible that the Atom middleware will pick up a few matching elements. -
If it's an Atom 1.0 feed, it is likely that the Atom 1.0 middleware will populate the results while the RSS 2.0 middleware will have no results.
For more information on how to write custom middleware, see @todo.
libxml
The libxml
settings that are used by default provide the best support for the widest variety of XML-based feeds. They are as follows:
libxml constant |
Description |
---|---|
LIBXML_BIGLINES |
Allows line numbers greater than 65535 to be reported correctly. |
LIBXML_COMPACT |
Activates small nodes allocation optimization. This may speed up your application without needing to change the code. |
LIBXML_HTML_NODEFDTD |
Sets HTML_PARSE_NODEFDTD flag, which prevents a default doctype being added when one is not found. |
LIBXML_HTML_NOIMPLIED |
Sets HTML_PARSE_NOIMPLIED flag, which turns off the automatic adding of implied html /body \ … elements. This is required otherwise parsing will fail. |
LIBXML_NOBLANKS |
Remove blank nodes. |
LIBXML_NOENT |
Substitute entities in the XML. |
LIBXML_NOXMLDECL |
Drop the XML declaration when saving a document. |
LIBXML_NSCLEAN |
Remove redundant namespace declarations. |
LIBXML_PARSEHUGE |
Sets XML_PARSE_HUGE flag, which relaxes any hardcoded limit from the parser. This affects limits like maximum depth of a document or the entity recursion, as well as limits of the size of text nodes. |
We explicitly do not set LIBXML_NOCDATA
because we want to preserve the CDATA-ness of those nodes. You can find the definitions for more libxml
constants by reading Predefined libxml constants.
If you want to change the libxml
parameters that are used, use any bitwise-OR value of the LIBXML_*
options you prefer.
<?php
use SimplePie\SimplePie;
$simplepie = (new SimplePie())
->setLibxml(LIBXML_HTML_NOIMPLIED | LIBXML_NOCDATA);