spanky/instagram

此包的最新版本(dev-master)没有提供许可证信息。

PHP 5.3+ 的 Instagram API 包装器

dev-master 2014-05-18 16:35 UTC

This package is not auto-updated.

Last update: 2024-09-28 15:18:34 UTC


README

#PHP 5.3+ 的 Instagram API [进行中]

一个简单、无框架绑定的 PHP 实现,用于 Instagram API 的包装器。

##安装

通过 composer 安装包

{
	"require": {
		"spanky/instagram": "dev-master"
	}
}

并在您的 PHP 文件中要求 Composer 自动加载器

	require 'vendor/autoload.php';

###Laravel 安装

如果您想使用 Laravel 4 与此包,可以使用包含的服务提供者和外观来处理引导代码。

首先,将服务提供者添加到 app/config/app.php 中的 providers 数组

'providers' => array(
	...
	'Spanky\Instagram\Laravel\InstagramServiceProvider',
);

然后,通过命令行将包中的配置文件发布到您的项目中

php artisan config:publish spanky/instagram

现在您应该在 app/config/packages/spanky/instagram/config.php 中看到配置文件,您可以在此处输入详细信息。

在配置文件中输入您的应用详情后,现在您可以访问带有 Instagram 别名的 Spanky\Instagram\Factory 类。

##用法

###身份验证

该包遵循此处描述的服务器端身份验证流程,描述如下。要在您的应用程序中设置身份验证,请按照以下步骤操作

  • 注册 Instagram 客户端,并注意生成的凭据。

  • 接下来,通过在 Spanky\Instagram\Factory 实例上调用 authorizor() 方法(如果未使用 Laravel,请传递配置详情)来创建 Spanky\Instagram\Authorizor 的实例,并调用 getAuthorizeUrl() 返回 Instagram 授权 URL。通过 PHP 或其他方式将用户重定向到此 URL。

<?php

use Spanky\Instagram\Factory as Instagram;

$config = array(

	'client_id' 	=> 'YOUR_CLIENT_ID',
	'client_secret'	=> 'YOUR_CLIENT_SECRET',
	'redirect_uri'	=> 'YOUR_REDIRECT_URL'
);

$authorize_url = Instagram::authorizor($config)->getAuthorizeUrl();

header("Location:{$authorize_url}");
  • 在授权后,用户将被重定向回您在注册客户端时指定的 URL,并在查询字符串中包含一个 code 参数。您需要将此代码传递给 Spanky\Instagram\Authorizor 类上的 getAccessToken() 方法以检索访问令牌。
<?php

use Spanky\Instagram\Factory as Instagram;

$config = array(

	'client_id' 	=> 'YOUR_CLIENT_ID',
	'client_secret'	=> 'YOUR_CLIENT_SECRET',
	'redirect_uri'	=> 'YOUR_REDIRECT_URL'
);

$token = Instagram::authorizor($config)->getAccessToken($_GET['code']);
  • 一旦您有了这个访问令牌,您就可以将其存储(在会话中、在数据库中等)并在进行 API 请求时检索它。
$_SESSION['instagram_access_token'] = $token;

header("Location:index.php");
// Redirect somewhere

###API 使用

一旦您拥有了访问令牌,您必须在 Spanky\Instagram\Instagram 类的实例上设置此令牌,然后才能发出任何请求。

<?php

use Spanky\Instagram\Factory as Instagram;

$instagram = Instagram::api();
$instagram->setAccessToken($_SESSION['instagram_access_token']);

###获取已授权用户

$user = $instagram->getAuthorizedUser();
// will return an instance of Spanky\Instagram\Entities\AuthorizedUser

echo $user->username; // the username of the authorized user
echo $user->profile_picture; // their profile picture
echo $user->mediaCount(); // the number of photos/videos they've uploaded

###通过 ID 获取用户

$user = $instagram->getUser(1010);
// will return an instance of Spanky\Instagram\Entities\User

获取关注某个用户的用户

$followers = $user->followers();
// returns an instance of Spanky\Instagram\Collections\UserCollection

foreach($followers as $follower) 
{
	// $follower is an instance of Spanky\Instagram\Entities\User
	echo $follower->username;
}

if ($followers->hasMoreItems()) 
{
	// If the user has more followers (results are paginated)
	$next_max_id = $followers->nextPageMaxId();
	// Get the next max id for the next request

	$moreFollowers = $user->followers(array('max_id' => $next_max_id));
	// Get the next round by passing in the max id
}

待续...