pgrimaud/instagram-user-feed

这是一个用于轻松获取任何动态并交互式地与Instagram(点赞、关注等)进行交互的PHP爬虫,无需OAuth。

7.0.3 2024-05-02 10:21 UTC

README

Build Status Packagist Coverage Status

Minimum PHP Version Last version Total Downloads

All Contributors

警告 2023-12-31 - 版本 7.0.0 将停止支持 PHP < 8.2

警告 2022-11-09 - Instagram 再次更改了他的DOM... 请更新到这个版本 6.16.4

警告 2022-06-01 - Instagram 更改了他的DOM内容和个人资料数据加载。请更新到这个版本 6.15.0

信息

轻松获取任何Instagram动态以及更多,无需OAuth,适用于PHP。

如果您喜欢或使用此包,请通过给此存储库加星标、关注 @pgrimaud成为赞助者 来分享您的喜爱。🙏💓

功能

  • 获取用户个人资料数据
  • 获取用户媒体
  • 获取用户故事
  • 获取用户高亮故事
  • 获取用户详细帖子
  • 获取关注者动态
  • 获取被关注者动态
  • 关注或取消关注用户
  • 点赞或取消点赞帖子
  • 获取标签的帖子
  • 获取帖子的评论
  • 获取直播信息
  • 获取Reels
  • 获取IGTV
  • 获取用户被标记的媒体
  • 获取高清个人资料图片
  • 向媒体发送消息
  • 新增:使用Cookies登录 🎉
  • 新增:获取连接用户的时间线 🎉

本版本可以使用 网络爬虫 获取 任何 Instagram动态。

⚠️ 版本 ^5.0 已不再维护。 ⚠️

安装

composer require pgrimaud/instagram-user-feed

变更日志

v7.0.0 - 2023-12-31:

  • 停止支持 PHP < 8.2

v6.16.6 - 2023-12-31:

  • 添加对Symfony 7的支持
  • 修复登录错误 "无法提取JSON数据"(《n1crask)

v6.16.5 - 2023-12-24:

  • 在获取个人资料后不再返回媒体。 $profile->getMedias() 在调用 $api->getProfile() 后将始终返回一个空数组。必须调用 $api->getMoreMedias($profile) 来返回前12个媒体(《cookieguru)
  • 同样适用于IGTV(《cookieguru)
  • 修复imap_delete第二个参数(《deepvision7)

v6.16 - 2022-08-02:

  • 使用Cookies登录。感谢 nsmle(示例 这里)🎉。
  • 获取连接用户的时间线。感谢 nsmle(示例 这里)🎉。
  • 添加对Symfony 6的完全支持
  • 添加对缺失属性的检查以避免错误

v6.15 - 2022-06-02:

  • ⚠️ ⚠️ 修复个人资料端点。请更新到最新版本!

v6.12 - 2021-09-20:

  • 添加在媒体上发送消息的方法(示例 这里)🎉。
  • 添加获取高清头像的方法(示例这里) 🎉。

v6.11 - 2021-09-13:

  • 添加获取用户被标记的媒体的方法(示例这里) 🎉。

v6.10 - 2021-09-03:

  • 添加获取IGTV的方法(示例这里) 🎉。
  • 添加获取Reels的方法(示例这里) 🎉。
  • 将脏测试拆分为多个文件。

v6.9 - 2021-09-02:

  • 添加获取Instagram直播信息的方法。感谢David-Kurniawan
  • 小修复。

v6.8 - 2021-07-08:

v6.7 - 2021-01-31:

  • 新功能:获取帖子标签
  • 新功能:获取帖子评论
  • 添加对PHP 8的支持。
  • 小改动和改进。

v6.6 - 2020-10-06:

  • 新功能:对帖子进行点赞取消点赞
  • 从故事中检索标签提及
  • 小改动和改进。

v6.5 - 2020-09-14:

  • 新功能:关注和取消关注用户。感谢@David-Kurniawan)
  • 小修复。

v6.4 - 2020-08-30:

  • 新功能:获取关注者和被关注者的动态。感谢@David-Kurniawan)
  • 小改进。

v6.3 -2020-07-03:

  • 使用IMAP配置绕过检查点挑战。

v6.2 - 2020-06-01:

  • 改进媒体抓取和缓存约束。

v6.1 - 2020-05-21:

  • 新功能:获取故事和亮点故事。

v6.0 - 2020-05-20

  • 请从^5.0升级以保持cookies会话稳定性。

版本^6.8: strict-origin-when-cross-origin

Facebook添加了新的CORS策略,您无法直接显示数据。

现在您可以在您的存储或服务器上下载媒体,直接在您的网站上提供服务。您可以在这里找到示例。

示例

<?php

// include vendor & classes

// random picture from instagram
$url = 'https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/156309873_1632221153646196_1273891214497323498_n.jpg?tp=1&_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_ohc=vbxGNZrjPmUAX8CIDdC&edm=ABfd0MgBAAAA&ccb=7-4&oh=ae5811c07f0e73b945eb203cd58c2101&oe=60EDD1EE&_nc_sid=7bff83'

// define directory
$downloadDir = __DIR__ . '/../assets'; // change it

$fileName = MediaDownloadHelper::downloadMedia($url, $downloadDir);
// file was downloaded here : __DIR__ . '/../assets/v-t51.2885-19-s150x150-156309873_1632221153646196_1273891214497323498_n.jpg

版本^6.3: 检查点挑战绕过

有些人可能在使用这个库登录时遇到麻烦。这发生在“旧”Instagram账户上,或者如果您在使用某些共享主机(不是所有,我不知道为什么...)。

现在您可以自动绕过检查点挑战。(用代码进行邮箱验证)。您可以在这里找到示例。

提示:您应该创建一个使用虚拟邮箱的虚拟Instagram账户来使用此功能。

它是如何工作的?

  1. 库将尝试登录
  2. 收到400错误“checkpoint_required”
  3. 触发邮箱验证
  4. 使用IMAP凭据连接到您的邮箱收件箱
  5. 等待Instagram验证邮件
  6. 从邮件中解析验证码
  7. 使用此代码向Instagram发送请求以完成验证
  8. 验证完成,然后自动保存会话*

*使用缓存驱动器保存会话在这里非常重要。Instagram会话有效期为... 1年。所以理论上,使用缓存驱动器和一个账户将只会触发一次真实的Instagram登录,然后长时间重用会话。

感谢 @ibnux@eldark 的帮助 🎉

版本 ^6.0:登录

在版本 ^6.0 中,登录现在 是强制性的,它将保存cookies(会话)以模拟对Instagram的“真实”请求。

他们改进了机器人检测,如果没有在头部请求中包含真实的会话数据,您的IP可能会被Instagram轻松软封禁。

然后,您不登录就无法获取大量数据。

提示:您只需创建或使用一个虚拟账户就可以轻松使用此包。

用法

6.0新增 缓存:此库实现了PSR-6以实现最大的互操作性。

<?php

use Instagram\Api;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$cachePool = new FilesystemAdapter('Instagram', 0, __DIR__ . '/../cache');

$api = new Api($cachePool);
$api->login('username', 'password'); // mandatory
$profile = $api->getProfile('robertdowneyjr');

echo $profile->getUserName(); // robertdowneyjr

echo $profile->getFullName(); // Robert Downey Jr. Official

基本用法

<?php

$api = new Api($cachePool);
$api->login('username', 'password');

$profile = $api->getProfile('robertdowneyjr');

print_r($profile);
Instagram\Hydrator\Component\Feed Object
(
    [id] => 1518284433
    [userName] => robertdowneyjr
    [fullName] => Robert Downey Jr. Official
    [biography] => @officialfootprintcoalition @coreresponse
    [followers] => 46382057
    [following] => 50
    [profilePicture] => https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s320x320/72702032_542075739927421_3928117925747097600_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_ohc=h2zGWoshNjUAX9ze3jb&oh=cf6441cfc3f258da3bf4cfef29686c7d&oe=5EEEC338
    [externalUrl] => http://coreresponse.org/covid19
    [private] => 
    [verified] => 1
    [mediaCount] => 453
        (
            [0] => Instagram\Model\InstagramMedia Object
                (
                    [id] => 2307655221969878423
                    [typeName] => GraphImage
                    [height] => 1350
                    [width] => 1080
                    [thumbnailSrc] => https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/sh0.08/e35/c0.180.1440.1440a/s640x640/96225997_178111910111734_5886065436455432375_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=1&_nc_ohc=GqcYpSEbz8gAX_GF1Ep&oh=1b293215142d407faca46a2fd28eab71&oe=5EF0EBDF
                    [link] => https://www.instagram.com/p/CAGcDKplv2X/
                    [date] => DateTime Object
                        (
                            [date] => 2020-05-12 22:06:01.000000
                            [timezone_type] => 3
                            [timezone] => Europe/Paris
                        )

                    [displaySrc] => https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e35/p1080x1080/96225997_178111910111734_5886065436455432375_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=1&_nc_ohc=GqcYpSEbz8gAX_GF1Ep&oh=6c19ddef96fdc07d7926b05e36cb2bed&oe=5EEED2CE
                    [caption] => The sweetest things are worth waiting forSusan and I are producing a @Netflix original series, Sweet Tooth, based on the comic by @Jefflemire. Can’t wait to share it with you all. 🦌 👦 @NXonNetflix @warnerbrostv #SweetTooth
                    [comments] => 3308
                    [likes] => 687988
                    [thumbnails] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [src] => https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e35/c0.180.1440.1440a/s150x150/96225997_178111910111734_5886065436455432375_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=1&_nc_ohc=GqcYpSEbz8gAX_GF1Ep&oh=24b300201afc0e0c82166c6288e0ed5b&oe=5EF00196
                                    [config_width] => 150
                                    [config_height] => 150
                                )

                            [1] => stdClass Object
                                (
                                    [src] => https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e35/c0.180.1440.1440a/s240x240/96225997_178111910111734_5886065436455432375_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=1&_nc_ohc=GqcYpSEbz8gAX_GF1Ep&oh=203d0a3d01d77a2978739c96eb67e607&oe=5EEF6DE0
                                    [config_width] => 240
                                    [config_height] => 240
                                )

                            [2] => stdClass Object
                                (
                                    [src] => https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e35/c0.180.1440.1440a/s320x320/96225997_178111910111734_5886065436455432375_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=1&_nc_ohc=GqcYpSEbz8gAX_GF1Ep&oh=7b9cee64460e1c9c501e59621e6ccfb2&oe=5EF18BE6
                                    [config_width] => 320
                                    [config_height] => 320
                                )

                            [3] => stdClass Object
                                (
                                    [src] => https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e35/c0.180.1440.1440a/s480x480/96225997_178111910111734_5886065436455432375_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=1&_nc_ohc=GqcYpSEbz8gAX_GF1Ep&oh=f3d8c31eca2d3c3ab6653b3ed3ebe4f4&oe=5EEFEAC0
                                    [config_width] => 480
                                    [config_height] => 480
                                )

                            [4] => stdClass Object
                                (
                                    [src] => https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/sh0.08/e35/c0.180.1440.1440a/s640x640/96225997_178111910111734_5886065436455432375_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=1&_nc_ohc=GqcYpSEbz8gAX_GF1Ep&oh=1b293215142d407faca46a2fd28eab71&oe=5EF0EBDF
                                    [config_width] => 640
                                    [config_height] => 640
                                )

                        )

                    [location] => 
                    [video] => 
                    [videoViewCount] => 0
                )
        ...
        
    [endCursor:Instagram\Model\InstagramProfile:private] => QVFEblBGclVyOEtCMmRLZkVxUUdVbmhsYXNMZmMmplNWtZRkJnRnZOSUdMM1BDRmt3ZA==
)

分页

如果您想在媒体上使用分页,只需调用 getMoreMedias 方法。

<?php

$api = new Api($cachePool);
$api->login($credentials->getLogin(), $credentials->getPassword());

$profile = $api->getProfile('twhiddleston');

print_r($profile->getMedias()); // 12 first medias

do {
    $profile = $api->getMoreMedias($profile);
    print_r($profile->getMedias()); // 12 more medias

    // avoid 429 Rate limit from Instagram
    sleep(1);
} while ($profile->hasMoreMedias());

故事

<?php

use Instagram\Api;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

$cachePool = new FilesystemAdapter('Instagram', 0, __DIR__ . '/../cache');

$api = new Api($cachePool);
$api->login('username', 'password'); // mandatory

$profile = $api->getProfile('starwars'); // we need instagram username
sleep(1);
$feedStories = $api->getStories($profile->getId());

$stories = $feedStories->getStories();

print_r($stories);

贡献者

感谢这些可爱的人们 (emoji key)

此项目遵循 all-contributors 规范。欢迎任何形式的贡献!

反馈

您发现了一个错误?您需要新功能?如果需要,您可以在 GitHub上创建问题,或者在 Twitter 上联系我。

许可证

在MIT许可证的条款下授权。