multidots / cakephp-instagram
CakePHP 3 的 Instagram 插件
Requires
- php: >=5.5.9
- cakephp/cakephp: >=3.3.2 <4.0.0
Requires (Dev)
- cakephp/cakephp-codesniffer: dev-master
- phpunit/phpunit: *
This package is not auto-updated.
Last update: 2020-05-14 13:12:16 UTC
README
要求
此插件有以下要求
- CakePHP 3.0.0 或更高版本。
- PHP 5.4.16 或更高版本。
安装
您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。
composer require multidots/cakephp-instagram
安装后,加载插件
Plugin::load('Instagram', ['bootstrap' => true]);
或者,您可以使用 shell 命令加载插件
$ bin/cake plugin load -b Instagram
使用方法
此插件提供了一个用于连接 Instagram API 的实用类。只需创建此类的实例并执行 API 调用即可。
Instagram API 使用 OAuth 2.0 协议,并要求所有经过身份验证的请求都需要 access_token
。
基本设置
在执行任何 API 调用之前,您需要在您的应用程序中创建 InstagramClient 类的新实例,并传递您的客户端 ID 和客户端密钥。
首先,编写一个包含客户端 ID、密钥和重定向 URL 的配置变量,例如:
$instaConfig = [ 'clientId' => 'your-client-id', 'clientSecret' => 'your-client-secret', 'redirectUri' => 'your-redirect-url' ]; Configure::write('Instagram.config', $instaConfig);
然后,创建一个实例并传递配置值,例如:
$instagramClient = new InstagramClient(Configure::read('Instagram.config'));
身份验证用户
为了进行经过身份验证的调用,Instagram API 要求请求中包含 access_token。要获取 access_token,您需要将用户重定向到 Instagram 身份验证 URL。此插件提供 Helper 来生成您的用户的身份验证 URL。在您的应用程序中加载 Instagram Helper 并使用其 getAuthUrl()
方法生成 URL。
在您的 AppView 中:
$this->loadHelper('Instagram.Instagram');
在您的模板中:
<a href="<?= $this->Instagram->getAuthUrl(); ?>">Authorize with Instagram</a>
处理响应中的 access_token
用户成功身份验证后,Instagram 将重定向到您指定的重定向 URI 并携带 code
。在您的操作中,通过代码获取 access_token 进行 API 调用,例如:
$response = $instagramClient->getAccessToken($this->request->query('code')); $accessToken = $response->access_token; // You may want to store it into database for future use
设置 access_token
一旦您从 Instagram 获取了 access_token
,通过 accessToken()
方法设置它。
$instagramClient = new InstagramClient(Configure::read('Instagram.config')); $instagramClient->accessToken('your-access-token');
现在可以准备进行经过身份验证的 API 调用了。
API 方法
目前,此插件提供以下 API 方法。
用户
个人资料
获取关于自我/特定用户的个人资料信息。支持以下参数
- user_id: 用户 ID(如果未设置,则使用 self)
$instagramClient = new InstagramClient($options); $instagramClient->accessToken($accessToken); // own profile $response = $instagramClient->getProfile(); // specific user profile $params = ['user_id' => 1]; $response = $instagramClient->getProfile($params);
最近媒体
获取用户发布的最新媒体。支持以下参数
- user_id: 用户 ID(如果未设置,则使用 self)
- url: API地址(通常用于分页(下一页地址),如果设置了其他
- 选项,则将被忽略)
- count: 返回的媒体数量
- min_id: 返回大于此min_id的媒体
- max_id: 返回小于此max_id的媒体
$instagramClient = new InstagramClient($options); $instagramClient->accessToken($accessToken); $params = ['count' => 20]; $response = $instagramClient->getMedia($params);