fishmarket / instaphp
由 sesser (Randy) 开发的 Instaphp 的 Composer 兼容版本
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-28 14:44:13 UTC
README
这是由 sesser (Randy) 编码的 Instaphp 的 Composer 兼容版本,可以在这里找到。
入门指南
添加到 composer.json 并安装/更新
{ "require": { "fishmarket/instaphp": "*" } }
index.php
<?php require 'vendor/autoload.php'; use Instaphp\Instaphp; // Config $config = array( 'client_id' => 'YOUR_CLIENT_ID' ); // Get an instance of the Instaphp object $api = Instaphp::Instance(null, $config); // Get the response for Popular media $response = $api->Media->Popular();
或者,您可以提供一个访问令牌
$api = Instaphp::Instance('YOUR_ACCESS_TOKEN');
您可以像上面那样覆盖默认配置。以下是快速参考的默认值
public $configDefaults = array( 'version'=> 'v1', 'endpoint'=> 'https://api.instagram.com', 'endpoint_timeout'=> '10', 'client_id'=> null, 'client_secret'=> null, 'oauth_path'=> '/oauth/authorize/?client_id={client_id}&response_type=code&redirect_uri={redirect_uri}', 'oauth_token_path'=> 'oauth/access_token', 'redirect_uri'=> '', );
其余的文档遵循下面的原始 Instaphp 文档
Instaphp v1.0 (下面的原始 README)
本软件许可协议为 The MIT License。请参阅名为 'LICENSE' 的文件以获取更多详细信息。
关于
Instaphp 是一个小型 PHP 库,用于访问 Instagram 的 API。其主要目标是易于使用、轻量级并且尽可能少地依赖。它目前仅与 PHP 5 >= 5.3 兼容,但使用了很少的 5.3 特性,并且将其转换为版本 < 5.3 是相对简单的。
Instaphp 目前被用于instaview.me。
快速入门指南
为了了解库的工作原理,最好理解 API 提供的各种 端点。Instagram API 目前有八个 "端点",可以从中检索系统中的数据
- 用户
- 关系
- 媒体
- 评论
- 点赞
- 标签
- 位置
- 地理(这是一个基于订阅的端点,在 Instaphp 中尚不可用)
实际上,这八个端点可以总结为四个主要端点
- 用户
- 媒体
- 标签
- 位置
关系实际上是用户的属性。评论和点赞都是媒体的属性。地理也是媒体的属性,但该特定端点仅适用于为特定位置创建的订阅。目前,Instaphp 没有支持订阅的机制。
这四个主要 "端点" 是本库的基础,并允许对从 Instagram 拉取的各种数据进行良好的分离。
示例:获取当前的热门照片
//-- Include our library
include_once 'instaphp/instaphp.php';
//-- Get an instance of the Instaphp object
$api = Instaphp\Instaphp::Instance();
//-- Get the response for Popular media
$response = $api->Media->Popular();
//-- Check if an error was returned from the API
if (empty($response->error))
foreach ($response->data as $item)
printf('<img src="%s" width="%d" height="%d" alt="%s">', $item->images->thumbnail->url, $item->images->thumbnail->width, $item->images->thumbnail->height, empty($item->caption->text) ? 'Untitled':$item->caption->text);
示例:认证
Instagram 使用 OAuth 进行用户认证。这意味着您点击一个链接到他们的网站,登录到他们的系统,并代表您授予应用程序访问权限。这是一个处理认证的常见方案,不需要通过(可能是)不安全的线路(例如非 https)传递敏感数据(例如您的用户名和密码)。
对于 Instaphp,您必须有一个 API 密钥、API 密钥和回调 URL,OAuth 才能正常工作。基本流程如下
- 用户点击 "登录" 链接
- 用户到达 Instagram 的网站并输入用户名/密码
- 在成功登录后,用户会被问及是否要授予应用程序对其照片的访问权限
- 如果用户允许访问,Instagram 将用户重定向到应用程序的回调 URL 并附带一个代码
- 应用程序使用代码调用 API 以验证认证
- 然后,应用程序会获得一个访问令牌来 "签名" 对 API 的调用
以下是这个过程的样子
//-- The oAuth URL can be found in the Config object
$oAuthUrl = Instaphp\Config::Instance()->GetOAuthUri();
<!-- Here's a link -->
<a href="<?php echo $oAuthUrl ?>">Login</a>
//-- To authenticate, simply grab the code in your callback url
$code = $_GET['code'];
if (!empty($code)) {
//-- Create an Instaphp instance
$api = Instaphp\Instaphp::Instance();
//-- Authenticate
$response = $api->Users->Authenticate($code);
//-- If no errors, grab the access_token (and cookie it, if desired)
if (empty($response->error)) {
$token = $response->auth->access_token;
setcookie('instaphp', $token, strtotime('30 days'));
//-- once you have a token, update the Instaphp instance so it passes the token for future calls
$api = Instaphp\Instaphp::Instance($token);
}
}