yuzuru-s/parse-rss

这个PHP库可以轻松解析xml文件,特别是RSS1.0、RSS2.0和ATOM。

1.0.6 2017-01-30 14:10 UTC

This package is not auto-updated.

Last update: 2024-09-15 01:55:57 UTC


README

Coverage Status Build Status Stable Version Download Count License

此解析器可以轻松处理RSS,无需区分RSS1.0、RSS2.0和ATOM之间的差异,并获取必要的最小值。

  1. 站点名称
  2. 站点URL
  3. 文章标题
  4. 文章URL
  5. 文章描述
  6. 文章发布日期
  7. 文章缩略图

此缩略图由描述中包含的og:img和img标签组成。

需求

  • PHP
    • =5.5 >=5.6, >=7.0

  • ext-xml
  • ext-curl
  • Composer

安装

  • 使用composer
{
    "require": {
       "yuzuru-s/parse-rss": "1.0.*"
    }
}
$ php composer.phar update yuzuru-s/parse-rss --dev

如何使用

请查看示例代码

<?php
require __DIR__ . '/../vendor/autoload.php';
use YuzuruS\Rss\Feed;

$url = 'http://blog.livedoor.jp/dqnplus/index.rdf';
$ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36';
$res = Feed::load($url, $ua, true);

echo 'Title:' . $res['channel']['title'] . "\n";
echo 'Link:' . $res['channel']['link'] . "\n";

foreach ($res['item'] as $r) {
	echo "\t" . 'Article Title:' . $r['title'] . "\n";
	echo "\t" . 'Article Description:' . $r['description'] . "\n";
	echo "\t" . 'Article Date:' . $r['date'] . "\n";
	echo "\t" . 'Article og:img:' . $r['image']['ogimg'] . "\n";
	foreach ($r['image']['img'] as $i) {
		echo "\t\t" . 'Desc:img:' . $i . "\n";
	}
}

输出

array(2) {
  'channel' =>
  array(2) {
    'title' =>
    string(27) "site name"
    'link' =>
    string "site url"
  }
  'item' =>
  array(15) {
    [0] =>
    array(5) {
      'title' =>
      string "title"
      'link' =>
      string"url"
      'date' =>
      string "date"
      'description' =>
      string "description"
      'image' =>
      array(2) {
        'ogimg' =>
        string(58) "img url"
        'img' =>
        array(3) {
          [0] =>
          string(58) "img url1"
          [1] =>
          string(58) "img url2"
          [2] =>
          string(94) "img url3"
        }
      }
    }
    [1] =>
    array(5) {
...

您还可以启用缓存

Feed::$cacheDir = __DIR__ . '/tmp';
Feed::$cacheExpire = '5 hours';

如何运行单元测试

使用默认设置运行。

% vendor/bin/phpunit -c phpunit.xml.dist

目前已在PHP 7.0.0上进行了测试

历史

  • 1.0.1
    • 错误修复
  • 1.0.0
    • 发布

许可证

版权所有 (c) 2016 YUZURU SUZUKI。有关更多详细信息,请参阅MIT-LICENSE。

版权