instagram-user-feed:基于 PHP 的 Instagram 数据获取工具项目

This is a scrapper to easily fetch any feed and interact with Instagram (like, follow, etc.) without OAuth for PHP.

分支2Tags74
文件最后提交记录最后更新时间
4 年前
4 年前
8 年前
3 年前
3 年前
3 年前
3 年前
4 年前
8 年前
2 年前
3 年前

Instagram 用户动态 PHP 插件

构建状态 Packagist 覆盖率状态

最低 PHP 版本 最新版本 总下载量

所有贡献者

警告 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

更新日志

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会话。

Version ^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:Checkpoint 挑战绕过

部分用户在使用这个库登录时可能会遇到问题。这通常发生在“老旧”的 Instagram 账户上,或者当你在某些共享主机(并非全部,我不清楚原因……)上使用时。

现在您可以通过自动绕过 checkpoint 挑战来解决这个问题。(通过邮件验证码进行邮箱验证)您可以在此处找到一个示例 here

小贴士:您应该使用一个虚拟邮箱创建一个虚拟的 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 for…Susan 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);

贡献者

感谢以下优秀的人们(表情键):

Pierre Grimaud
Pierre Grimaud

💻
Jan Östlund
Jan Östlund

💻
Tim Bond
Tim Bond

💻
Dlinny
Dlinny

🐛
René
René

🐛
ikiselev1989
ikiselev1989

🐛
Pezhvak
Pezhvak

💻
David Greminger
David Greminger

💻
Nana YAMANE
Nana YAMANE

💻
David Kurniawan
David Kurniawan

💻
gtapps
gtapps

💻
Chun-Sheng, Li
Chun-Sheng, Li

⚠️
schelmo
schelmo

💻
Mojtaba
Mojtaba

💻
Georgy Gusev
Georgy Gusev

💻
Jibran Ijaz
Jibran Ijaz

💻
Julien Cousin-Alliot
Julien Cousin-Alliot

💻
n-richaud
n-richaud

🐛
Dmitry Afonin
Dmitry Afonin

💻
Andrei Firoiu
Andrei Firoiu

🐛
Fiki Pratama
Fiki Pratama

💻
Carl
Carl

🐛
Peter Kampjes
Peter Kampjes

💻
Bob Brown
Bob Brown

📖
nagajoomlapro
nagajoomlapro

💻
cleveng
cleveng

💻
BMRG14
BMRG14

💻
Sinan Portakal
Sinan Portakal

💻

本项目遵循 all-contributors 规范。欢迎各种形式的贡献!

反馈

你发现了一个错误?你需要一个新功能?如有需要,你可以创建一个issue,或者通过推特联系我。

许可

根据MIT许可证的条款授权。

项目介绍

This is a scrapper to easily fetch any feed and interact with Instagram (like, follow, etc.) without OAuth for PHP.

定制我的领域