forensic / feed-parser
一个完全集成的ATOM、RSS和RDF聚合源解析器
v1.2.2
2020-06-11 22:21 UTC
Requires
- php: >=7.1
Requires (Dev)
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ^7.3
- squizlabs/php_codesniffer: ^3.3
- dev-master
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.0
- v1.0.0
- dev-dependabot/npm_and_yarn/qs-6.5.3
- dev-dependabot/npm_and_yarn/decode-uri-component-0.2.2
- dev-dependabot/npm_and_yarn/semantic-release-19.0.3
- dev-dependabot/npm_and_yarn/path-parse-1.0.7
- dev-dependabot/npm_and_yarn/normalize-url-4.5.1
- dev-dependabot/npm_and_yarn/hosted-git-info-2.8.9
- dev-dependabot/npm_and_yarn/handlebars-4.7.7
- dev-dependabot/npm_and_yarn/ssri-6.0.2
- dev-dependabot/npm_and_yarn/y18n-3.2.2
- dev-dependabot/npm_and_yarn/ini-1.3.7
- dev-dependabot/npm_and_yarn/npm-registry-fetch-4.0.5
- dev-dependabot/npm_and_yarn/npm-6.14.6
This package is auto-updated.
Last update: 2024-09-11 02:02:47 UTC
README
PHP Feed Parser是一个完全集成的网络聚合源解析器。它可以成功解析包括RSS、ATOM和RDF在内的所有三种主要聚合源。
它为所有三种支持的聚合源类型产生干净、统一的解析结果。它是可配置的、轻量级的、经过完全测试的,并允许从三个不同的源解析聚合源,包括从URL、文件或字符串解析。
入门指南
通过composer安装:
composer require forensic/feed-parser
创建实例,如下所示
//include composer autoload.php require 'vendor/autoload.php'; //use the project's Parser module use Forensic\FeedParser\Parser; //create an instance $parser = new Parser(); //parse yahoo rss news feed $feed = $parser->parseFromURL('https://www.yahoo.com/news/rss/mostviewed'); //access feed properties echo $feed->type; //1 for RSS, 2 for ATOM, 3 for RDF echo $feed->title; echo $feed->link; echo $feed->lastUpdated; echo $feed->generator; echo $feed->description; echo $feed->image->src; .... //access items $items = $feed->items; foreach ($items as $item) { //access feed item properties echo $item->link; echo $item->content; echo $item->textContent; // the same as content, but all html tags are stripped out echo $item->createdAt; echo $item->lastUpdated; echo $item->category; echo $item->image->src; echo $item->image->title; ..... }
导出聚合源为JSON
您还可以将解析后的聚合源导出为json,如下所示。这也有助于您查看解析后的聚合源中可访问的所有属性。
//create an instance $parser = new Parser(); //parse yahoo rss news feed $feed = $parser->parseFromURL('https://www.yahoo.com/news/rss/mostviewed'); header('Content-Type: application/json'); //export whole feed echo $feed->toJSON(); //export a single item echo $feed->items[0]->toJSON();
解析器选项
在创建实例时,可以通过以下配置选项传递,也可以通过指定的公共方法设置
//constructor signature new Parser( string $default_lang = 'en', bool $remove_styles = true, bool $remove_scripts = true );
-
default_lang:
此选项设置在xml文档中找不到语言条目时应使用的默认聚合源语言属性。
$parser = new Parser(); $parser->setDefaultLanguage('fr');
-
remove_styles:
此选项说明是否应从任何聚合源条目的内容中移除样式。样式包括HTML
style
元素和属性。默认为true。$parser = new Parser(); $parser->removeStyles(true);
-
remove_scripts:
此选项说明是否应从任何聚合源条目的内容中移除脚本。脚本包括HTML
script
元素和事件处理器on-prefixed
元素属性,如onclick
。默认为true。$parser = new Parser(); $parser->removeScripts(true);
测试FeedParser
要本地测试或为此项目做出贡献,您至少需要安装php 7.1、xDebug、composer和npm,然后按照以下步骤操作
克隆此仓库:
git clone https://github.com/teclone/php-feed-parser && php-feed-parser
安装依赖项:
composer install && npm install
运行测试:
vendor/bin/phpunit