gnahotelsolutions / laravel-instagram-feed
抓取给定认证的Instagram账号的动态
Requires
- php: ^8.0 | ^8.1
- ext-json: *
- guzzlehttp/guzzle: ^7.0.1
- illuminate/config: ^7.0 | ^8.0 | ^9.0 | ^10.0
- illuminate/console: ^7.0 | ^8.0 | ^9.0 | ^10.0
- illuminate/database: ^7.0 | ^8.0 | ^9.0 | ^10.0
- illuminate/http: ^7.0 | ^8.0 | ^9.0 | ^10.0
- illuminate/mail: ^7.0 | ^8.0 | ^9.0 | ^10.0
- illuminate/support: ^7.0 | ^8.0 | ^9.0 | ^10.0
Requires (Dev)
- orchestra/testbench: ^6.0 | ^7.0
This package is auto-updated.
Last update: 2024-09-23 11:50:03 UTC
README
轻松地将Instagram动态包含到您的项目中。
该包的目的是尽可能简化并使Instagram动态的集成变得用户友好,使用Instagram的基本展示API。该包将动态存储在您的缓存中,并提供按您的计划刷新动态的能力。
注意 此包需要PHP 8。如果您不习惯使用PHP 8,则可以继续使用v2。
安装
composer require dymantic/laravel-instagram-feed
注意 如果您是从v2.*版本升级,请参阅升级指南,因为有一些重大更改。 另请注意 此版本需要PHP 8及以上版本,所以如果您仍在使用PHP 7且在安装时未指定版本,composer将拉取^v2版本,在这种情况下,您应该阅读此页面。
教程
这个项目的一些部分可能有点难以理解,尤其是如果您不熟悉OAuth流程。我包括了一个教程来尝试使事情更清晰。对于反馈或改进,我会非常感激。
开始之前
要使用Instagram基本展示API,您需要设置一个具有正确权限的Facebook应用。如果您还没有,请转到Facebook开发者文档并按照说明操作。
Instagram媒体的处理方式
通过此API,Instagram提供了三种媒体类型:图片、视频和轮播图。该包将它们简化为图片和视频的动态。如果您不想包含任何视频,可以使用ignore_video
配置选项。对于轮播图项目,使用轮播图的第一项。如果忽略视频,并且存在第一张图片,则将使用第一张图片。轮播图项目具有children
属性,其中包含实际的轮播图项目。
关于忽略视频的说明
如果您选择忽略视频,则动态的大小可能小于您请求的限制。如果您预计会忽略视频帖子,则可能需要增加获取的帖子数量(请参阅下文的“获取动态”)。
设置
使用php artisan vendor:publish
发布必要的迁移和配置,然后使用php artisan migrate
运行迁移。
配置
如上所述发布供应商资产将为您提供一个config/instagram-feed.php
文件。在您开始之前,您需要添加配置。文件中的注释解释了每个设置。
// config/instagram-feed.php <?php return [ /* * The client_id from registering your app on Instagram */ 'client_id' => 'YOUR INSTAGRAM CLIENT ID', /* * The client secret from registering your app on Instagram, * This is not the same as an access token. */ 'client_secret' => 'YOUR INSTAGRAM CLIENT SECRET', /* * The base url used to generate to auth callback route for instagram. * This defaults to your APP_URL, so normally you may leave it as null */ 'base_url' => null, /* * The route that will respond to the Instagram callback during the OAuth process. * Only enter the path without the leading slash. You need to ensure that you have registered * a redirect_uri for your instagram app that is equal to combining the * app url (from config) and this route */ 'auth_callback_route' => 'instagram/auth/callback', /* * On success of the OAuth process you will be redirected to this route. * You may use query strings to carry messages */ 'success_redirect_to' => 'instagram-auth-success', /* * If the OAuth process fails for some reason you will be redirected to this route. * You may use query strings to carry messages */ 'failure_redirect_to' => 'instagram-auth-failure' /* * You may filter out video media types by setting this to true. Carousel media * will become the first image in the carousel, and if there are no images, then * the entire carousel will be ignored. */ 'ignore_video' => false, /* * You may set an email address below if you wish to be notified of errors when * attempting to refresh the Instagram feed. */ 'notify_on_error' => null, ];
配置文件
此包提供了一个与Instagram配置文件/账号对应的GNAHotelSolutions\InstagramFeed\Profile
模型。您需要为要在您的应用/网站上使用的每个动态创建一个配置文件。一旦创建了一个配置文件,您需要授权该配置文件,然后才能获取其动态。
创建配置文件
您可以通过以下方式在代码中以编程方式创建个人资料。您只需要提供唯一的用户名即可。
$profile = \GNAHotelSolutions\InstagramFeed\Profile::new('my profile');
对于单个项目只创建一个个人资料是一种相当常见的用例,因此此包包括一个Artisan命令来快速创建个人资料,这样您就无需为用户提供必要的UI来执行此操作。
获取授权
一旦您有了个人资料,您可以调用其上的 getInstagramAuthUrl()
方法以获取一个用于用户进行认证的链接。当用户访问该链接时,他们可以授予您的应用程序访问权限(或不授予)。如果一切顺利,用户将被重定向回您在配置文件中设置的 success_redirect_to
路由。如果未授予访问权限,您将被重定向到您配置的 failure_redirect_to
路由。
如果您没有正确设置client_id和/或client_secret,或者您的Instagram应用程序不接受用户,Instagram将不会进行任何重定向,并且您的用户将看到Instagram的错误。
刷新访问令牌
API的长效访问令牌在60天后过期。此包包括一个Artisan命令来为您处理此操作,您只需确保它至少每60天运行一次。该命令是 php artisan instagram-feed:refresh-tokens
获取内容
一旦您的个人资料已认证,您可以直接从 InstagramFeed
类获取内容,或者首先获取个人资料,然后调用其上的内容方法。
$feed = \GNAHotelSolutions\InstagramFeed\InstagramFeed::for('my profile');
或者
$profile = \GNAHotelSolutions\InstagramFeed\Profile::for('my profile') $feed = $profile?->feed();
获取内容后,它将被永久缓存,并且相同的缓存结果将在您 刷新
内容之前返回。通常这是通过计划好的cron作业完成的。
限制内容数量
默认情况下,内容数量为20。在获取内容时,您可以传递一个可选的整数参数,如下所示: \GNAHotelSolutions\InstagramFeed\InstagramFeed::for('my profile', 15)
或 $profile?->feed(15)
以将内容限制在指定数量。
获取所有帖子(无限制)
如果您想获取所有帖子(最多1000条),您可以将 null
作为限制参数传递给 refreshFeed
或 feed
方法。该包将进行多次请求以获取您之前发布的所有帖子,最多1000条。然后您可以使用帖子的时间戳进行排序、分页等。
设置限制和缓存
当内容来自缓存时,限制将不会有影响。如果您想将限制更改为之前使用的限制,您将必须 刷新
内容。
设置限制和忽略视频
如果您选择忽略视频,内容大小可能小于您请求的限制。如果您预计要忽略视频帖子,您可能需要增加获取的帖子数量。
更新内容
您可以像获取内容那样刷新内容
$feed = \GNAHotelSolutions\InstagramFeed\InstagramFeed::for('my profile')->refresh();
或者
$profile = \GNAHotelSolutions\InstagramFeed\Profile::for('my profile') $feed = $profile?->refreshFeed();
刷新内容返回一个 InstagramFeed
实例,但是,与获取常规内容不同,如果发生错误,此方法可以抛出异常。这种想法是在后台某处刷新您的内容,通常在计划好的作业中。此包包括一个Artisan命令 php artisan instagram-feed:refresh
,该命令将刷新所有已授权的个人资料,并处理可能发生的错误。如果您在配置中设置了电子邮件地址,则将在发生错误时通知该地址。建议使用Laravel的计划功能按需运行此命令。
使用内容
一旦您拥有一个数据源,您可以将其发送到您的视图,其中可以遍历数据源,数据源中的每个项目都是一个 InstagramMedia
类的实例。
// somewhere in a Controller public function show() { $feed = \GNAHotelSolutions\InstagramFeed\InstagramFeed::for('my profile'); return view('instagram-feed', ['instagram_feed' => $feed]); } // instagram-feed.blade.php @foreach($instagram_feed as $post) <img src={{ $post->url }} alt="A post from my instagram"> @endforeach
如果您想要对数据源的项目有更多控制,您可以在数据源上调用 collect
方法来获取数据源项作为 Laravel 集合。
数据源项
每个数据源项是一个 InstagramMedia
类的实例,并提供以下属性和方法