kalimeromk/rssfeed

Laravel 的简单 RSS 订阅阅读器

安装: 88

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:package

v3 2024-05-30 15:20 UTC

This package is auto-updated.

Last update: 2024-08-30 17:48:34 UTC


README

此包提供了一种轻松解析 RSS 订阅并将其保存到应用程序中的方法。它提供了诸如获取 RSS 订阅的完整内容、保存订阅项中找到的图片以及获取订阅中每个项目的完整内容等功能。

功能

  1. 解析多个 RSS 订阅。
  2. 将 RSS 订阅项中的图片保存到存储位置。
  3. 检索 RSS 订阅中每个项目的完整内容。

需求

  • PHP 7.4 或更高版本
  • SimplePie PHP 库 1.8 或更高版本

安装

您可以通过 Composer 安装此包:

composer require kalimeromk/rssfeed


This package uses Laravel's auto-discovery feature, so you don't need to register the service provider.

## Configuration

This package supports optional configuration.

You can publish the configuration file using:

``` bash 
php artisan vendor:publish --provider="Kalimeromk\Rssfeed\RssFeedServiceProvider" --tag="config"

这将发布一个 rssfeed.php 配置文件到您的配置目录。在这里,您可以设置内容元素的 XPaths。

return [
    'image_storage_path' => 'images',
];

在此配置文件中

  • image_storage_path: 指定 RSS 订阅项中的图片应存储的路径。

致谢

此包由 KalimeroMK 创建。

许可证

MIT 许可证(MIT)。请参阅许可证文件以获取更多信息。

用法

以下是使用此包的示例。

namespace App\Http\Controllers;

use Kalimeromk\Rssfeed\RssFeed;
use Illuminate\Http\Request;

class RssFeedController extends Controller
{
    public function index()
    {
        $feed = RssFeed::parseRssFeeds('https://example.com/feed/');
        
       $result = [
            'title' => $feed->get_title(),
            'description' => $feed->get_description(),
            'permalink' => $feed->get_permalink(),
            'link' => $feed->get_link(),
            'copyright' => $feed->get_copyright(),
            'language' => $feed->get_language(),
            'image_url' => $feed->get_image_url(),
            'author' => $feed->get_author()
        ];
        foreach ($feed->get_items(0, $feed->get_item_quantity()) as $item) {
            $i['title'] = $item->get_title();
            $i['description'] = $item->get_description();
            $i['id'] = $item->get_id();
            $i['content'] = $item->get_content();
            $i['thumbnail'] = $item->get_thumbnail() ?: $rssFeed->extractImageFromDescription($item->get_content());
            $i['category'] = $item->get_category();
            $i['categories'] = $item->get_categories();
            $i['author'] = $item->get_author();
            $i['authors'] = $item->get_authors();
            $i['contributor'] = $item->get_contributor();
            $i['copyright'] = $item->get_copyright();
            $i['date'] = $item->get_date();
            $i['updated_date'] = $item->get_updated_date();
            $i['local_date'] = $item->get_local_date();
            $i['permalink'] = $item->get_permalink();
            $i['link'] = $item->get_link();
            $i['links'] = $item->get_links();
            $i['enclosure'] = $item->get_enclosure();
            $i['audio_link'] = $item->get_enclosure() ? $item->get_enclosure()->get_link() : null;
            $i['enclosures'] = $item->get_enclosures();
            $i['latitude'] = $item->get_latitude();
            $i['longitude'] = $item->get_longitude();
            $i['source'] = $item->get_source();

            $result['items'][] = $i;
        }
        
        return $result;
    }
}

保存图片

您可以使用 saveImagesToStorage 方法保存 RSS 订阅项中找到的图片。此方法接受一个包含图片 URL 的数组,并返回一个包含已保存图片名称的数组。

$images = [
    'http://example.com/image1.jpg',
    'http://example.com/image2.jpg',
];
$savedImageNames = $rssFeed->saveImagesToStorage($images);

作业

如果您需要调度 RssFeed 作业,可以按以下方式操作

use Kalimeromk\Rssfeed\Jobs\RssFeedJob;

$feedUrls = ['https://example.com/rss'];

RssFeedJob::dispatch($feedUrls);