oh /instagram-bundle
使用sesser (Randy)的Instaphp作为Symfony2服务
dev-master
2014-05-27 11:41 UTC
Requires
- php: >=5.3.2
- symfony/framework-bundle: *
This package is not auto-updated.
Last update: 2024-09-14 12:08:54 UTC
README
使用Instaphp(由sesser编写)作为Symfony2服务
安装
按照常规方法安装此包,通过添加到composer.json文件
"oh/instagram-bundle": "dev-master"
安装上述模块后,您需要更新您的composer
run the command “php composer.phar update”
在app/AppKernel.php
中注册此包
// app/AppKernel.php
public function registerBundles()
{
return array(
// ...
new Oh\InstagramBundle\OhInstagramBundle(),
);
}
在app/config/config.yml
中添加以下行
imports:
- { resource: @OhInstagramBundle/Resources/config/services.yml }
将以下配置添加到您的parameters.yml文件中
instagram_api_client_id: YOUR_API_ID
instagram_api_client_secret: YOUR_API_SECRET
instagram_timeout: 30
instagram_connect_timeout: 8
如果对提供的路由没有问题,请将以下内容添加到app/config/routing.yml
OhInstagramBundle:
resource: "@OhInstagramBundle/Resources/config/routing.yml"
prefix: /
使用(控制器)
//finding a user
$query = "snoopdogg";
$api = $this->get('instaphp');
$response = $api->Users->Find($query);
$userInfo = $response->data[0];
// getting all the pages of results
do {
$pages[] = $response->data;
while($response = $response->getNextPage());
您还可以测试用户是否已登录。
//is a user logged in?
$loggedIn = $this->get('instaphp_token_handler')->isLoggedIn();
使用(Twig)
视图中包含登录按钮。只需在您的Twig模板中实现此行即可
{{ render(controller('OhInstagramBundle:Instagram:instagramLoginStatus')) }}
您应该设置Instagram API账户以回调到"OHInstagramBundle_callback"路由,您可以自行设置或使用提供的路由 - "/instaphp/callback"。
Instagram认证令牌
包含2个TokenHandlers。
-
CookieToken - Instagram认证代码存储在cookie中
services: instaphp_token_handler: class: Oh\InstagramBundle\TokenHandler\CookieToken
-
UserToken - Instagram认证代码存储在User实体中。必须在您的User上实现setInstagramAuthCode()和getInstagramAuthCode()方法。当从Instagram返回登录调用时,代码被设置,并在Handler中持久化和刷新用户。
services: instaphp_token_handler: class: Oh\InstagramBundle\TokenHandler\UserToken arguments: [@security.context, @doctrine.orm.default_entity_manager]
-
两者 - 如果无法从上下文中检索用户,它将在cookie中存储认证代码。
services: instaphp_user_token_handler: class: Oh\InstagramBundle\TokenHandler\UserToken arguments: [@security.context, @doctrine.orm.default_entity_manager] instaphp_cookie_token_handler: class: Oh\InstagramBundle\TokenHandler\CookieToken instaphp_token_handler: class: Oh\InstagramBundle\TokenHandler\TokenManager arguments: [@instaphp_user_token_handler, @instaphp_cookie_token_handler]
您还可以实现自己的TokenHandlerInterface以将认证代码存储在别处,例如会话等。
测试
@todo
致谢
- Ollie Harridge (ollietb) 作为作者。
- Randy (sesser) 为编写Instaphp脚本[https://github.com/sesser/Instaphp]