espresso-dev/instagram-basic-display-php

一个用于访问Instagram基本显示API的简单PHP类

v1.1.7 2024-09-23 17:57 UTC

This package is auto-updated.

Last update: 2024-09-23 18:02:47 UTC


README

由于Instagram基本显示API将于2024年12月4日弃用 https://developers.facebook.com/docs/instagram-basic-display-api/,因此此包不再维护。请使用Instagram API PHP包代替。

Instagram基本显示PHP API

Instagram基本显示API的简单PHP封装。基于Instagram-PHP-API,由Christian Metz开发。

Latest Stable Version License Total Downloads

Composer包可用。

需求

  • PHP 5.6或更高版本
  • cURL
  • Facebook开发者账户
  • Facebook应用

开始使用

要使用Instagram基本显示API,您需要注册一个Facebook应用并配置Instagram基本显示。按照入门指南操作。

安装

强烈建议使用Composer以保持更新尽可能顺畅。

$ composer require espresso-dev/instagram-basic-display-php

初始化类

use EspressoDev\InstagramBasicDisplay\InstagramBasicDisplay;

$instagram = new InstagramBasicDisplay([
    'appId' => 'YOUR_APP_ID',
    'appSecret' => 'YOUR_APP_SECRET',
    'redirectUri' => 'YOUR_APP_REDIRECT_URI'
]);

echo "<a href='{$instagram->getLoginUrl()}'>Login with Instagram</a>";

用户认证(OAuth2)

// Get the OAuth callback code
$code = $_GET['code'];

// Get the short lived access token (valid for 1 hour)
$token = $instagram->getOAuthToken($code, true);

// Exchange this token for a long lived token (valid for 60 days)
$token = $instagram->getLongLivedToken($token, true);

echo 'Your token is: ' . $token;

获取用户资料

// Set user access token
$instagram->setAccessToken($token);

// Get the users profile
$profile = $instagram->getUserProfile();

echo '<pre>';
print_r($profile);
echo '<pre>';

所有方法都将API数据作为json_decode()返回 - 因此您可以直接访问数据。

可用方法

设置Instagram

new Instagram(<数组>/<字符串>);

如果您想执行OAuth,请使用array

new InstagramBasicDisplay([
    'appId' => 'YOUR_APP_ID',
    'appSecret' => 'YOUR_APP_SECRET',
    'redirectUri' => 'YOUR_APP_REDIRECT_URI'
]);

一旦您有了令牌,只想返回只读数据,请使用string

new InstagramBasicDisplay('ACCESS_TOKEN');

获取登录URL

getLoginUrl(<数组>, <字符串>)

getLoginUrl(
    array(
        'user_profile', 
        'user_media'
    ),
    'state'
);

获取OAuth令牌(短效,有效期为1小时)

getOAuthToken($code, <true>/<false>)

true:仅返回OAuth令牌
false [默认]:返回OAuth令牌和认证用户的资料数据

交换OAuth令牌以获取长效令牌(有效期为60天)

getLongLivedToken($token, <true>/<false>)

true:仅返回OAuth令牌
false [默认]:返回OAuth令牌和认证用户的资料数据

在令牌过期前60天内刷新访问令牌

refreshToken($token, <true>/<false>)

true:仅返回OAuth令牌
false [默认]:返回OAuth令牌和令牌过期数据

设置/获取访问令牌

  • 设置访问令牌,用于后续方法调用:setAccessToken($token)
  • 获取访问令牌,如果您想将其存储以供以后使用:getAccessToken()

用户方法

认证方法

  • getUserProfile()
  • getUserMedia(<$id>, <$limit>)
    • 如果没有定义$id或等于'me',它将返回登录用户的媒体

媒体方法

认证方法

  • getMedia($id)
  • getMediaChildren()

分页

由于getUserMedia端点有一个最大结果范围,因此将limit参数增加到99以上的限制不会有所帮助。您可以使用分页来获取此端点的更多结果。

将对象传递给pagination()方法以接收下一数据集

$media = $instagram->getUserMedia();

$moreMedia = $instagram->pagination($media);