valga / instagram-rest-api
此包已被废弃且不再维护。未建议替代包。
关于此包的最新版本(0.1.1)没有可用的许可证信息。
Instagram REST 和搜索 API 的 PHP 包装器
0.1.1
2017-06-17 09:53 UTC
Requires
- php: >=5.6
- guzzlehttp/guzzle: ^6.2
- netresearch/jsonmapper: ^1.1
- psr/log: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.3
- monolog/monolog: ^1.22
- phpunit/phpunit: ^6.2
This package is not auto-updated.
Last update: 2020-01-24 16:50:08 UTC
README
Instagram REST 和搜索 API 的 PHP 包装器。
安装
composer require valga/instagram-rest-api
基本用法
要使用 Instagram API,您需要创建一个应用程序(如果您还没有的话)。
$apiClient = new \InstagramRestApi\Client([ 'clientId' => 'YOUR_CLIENT_ID', 'clientSecret' => 'YOUR_CLIENT_SECRET', 'accessToken' => 'YOUR_ACCESS_TOKEN', // or null, if you don't have it yet 'enforceSignedRequests' => false, // or true, if you have enabled this feature ]);
获取访问令牌
try { $result = $apiClient->getAccessToken(); } catch (\Exception $e) { die(sprintf('Failed to obtain access token: %s', $e->getMessage())); } if ($result === null) { header('Location: '.$apiClient->getLoginUrl()); } else { printf('Your access token is: %s', $result->getAccessToken()); }
动态更改访问令牌
$apiClient->getAuth()->setAccessToken($newAccessToken);
从 API 端点获取数据
所有端点响应都有 getData()
方法,它返回所需的数据。
// Get a username of logged in user. $user = $apiClient->users->getSelf()->getData(); print_r($user->getUsername());
可用端点
$comments = $apiClient->comments; $comments->get($mediaId); $comments->add($mediaId, $text); $comments->delete($mediaId, $commentId); $locations = $apiClient->locations; $locations->get($locationId); $locations->getRecentMedia($locationId); $locations->search($lat, $lng); $likes = $apiClient->likes; $likes->get($mediaId); $likes->add($mediaId); $likes->delete($mediaId); $media = $apiClient->media; $media->getById($mediaId); $media->getByShortcode($mediaShortcode); $media->search($lat, $lng); $relationships = $apiClient->relationships; $relationships->approve($userId); $relationships->ignore($userId); $relationships->follow($userId); $relationships->unfollow($userId); $relationships->getStatus($userId); $relationships->getFollowing(); $relationships->getFollowers(); $relationships->getPendingUsers(); $subscriptions = $apiClient->subscriptions; $subscriptions->get(); $subscriptions->add($object, $aspect, $callbackUrl, $verifyToken); $subscriptions->deleteById($subscriptionId); $subscriptions->deleteByObject($object); $tags = $apiClient->tags; $tags->get($tag); $tags->getRecentMedia($tag); $tags->search($query); $users = $apiClient->users; $users->getSelf(); $users->getUser($userId); $users->getSelfRecentMedia(); $users->getUserRecentMedia($userId); $users->getSelfLikedMedia(); $users->search($query);
分页
只需调用 getNextPage()
方法,直到它返回 null
。
// Get all media of logged in user. $userMedia = []; $result = $apiClient->users->getSelfRecentMedia(); do { foreach ($result->getData() as $media) { $userMedia[] = $media; } } while (($result = $result->getNextPage()) !== null); print_r($userMedia);
高级用法
获取访问令牌
$scopes = ['public_content']; $requestData = $_GET; $redirectUrl = 'YOUR_CUSTOM_REDIRECT_URL'; $csrfToken = 'YOUR_CSRF_TOKEN'; try { $result = $apiClient->getAccessToken($request, $csrfToken, $redirectUrl); } catch (\Exception $e) { die(sprintf('Failed to obtain access token: %s', $e->getMessage())); } if ($result === null) { header('Location: '.$apiClient->getLoginUrl($scopes, $csrfToken, $redirectUrl)); die(); } else { printf('Your access token is: %s', $result->getAccessToken()); }
异常系统
此库抛出的所有高级异常都继承自 \InstagramRestApi\Exception\RequestException
。
RequestException
|- NetworkException - There was some error on network level.
|- InvalidResponseException - Response body is not a valid JSON object.
|- EndpointException
|- OAuthException
|- RateLimitException - You have exceeded rate limit.
|- MissingPermissionException - You don't have required scope.
|- InvalidSignatureException - Signature is missing or invalid.
|- InvalidTokenException - Invalid or expired token.
|- InvalidParametersException
|- NotFoundException - Requested object does not exist.
|- SubscriptionException
速率限制
$result = $apiClient->users->getSelfRecentMedia(); printf('%d/%d', $result->getRateLimitRemaining(), $result->getRateLimit());
日志记录和代理
我们使用 info
级别记录所有成功的请求及其响应,使用 error
级别记录失败的请求(如果有响应)。
$logger = new Monolog\Logger('instagram'); $logger->pushHandler(new Monolog\Handler\StreamHandler(__DIR__.'/logs/info.log', Monolog\Logger::INFO, false)); $logger->pushHandler(new Monolog\Handler\StreamHandler(__DIR__.'/logs/error.log', Monolog\Logger::ERROR, false)); $guzzleClient = new GuzzleHttp\Client([ 'connect_timeout' => 10.0, 'timeout' => 60.0, // Use proxy. 'proxy' => 'username:password@127.0.0.1:3128', // Disable SSL certificate validation. 'verify' => false, ]); $apiClient = new \InstagramRestApi\Client($config, $logger, $guzzleClient);