ollieltd/instagram-bundle

使用 sesser (Randy) 的 Instaphp 作为 Symfony2 服务。适应使用 Instaphp v2 作为依赖。

安装量: 19,239

依赖者: 0

建议者: 0

安全: 0

星级: 11

关注者: 3

分支: 12

开放问题: 8

类型:symfony-bundle

dev-master 2018-07-09 20:52 UTC

This package is not auto-updated.

Last update: 2024-09-24 22:40:33 UTC


README

使用 Instaphp(由 sesser 编写)作为 Symfony2 服务。适配使用 Instaphp v2 作为依赖。

安装

通过在 composer.json 中添加来按常规安装此包

"ollieltd/instagram-bundle": "dev-master"

在安装上述模块后,您需要更新您的 composer

composer update

app/AppKernel.php 中注册此包

// app/AppKernel.php
public function registerBundles()
{
    return [
        // ...
        new Oh\InstagramBundle\OhInstagramBundle(),
    ];
}

将以下内容添加到 app/config/config.yml

imports:
    - { resource: "@OhInstagramBundle/Resources/config/services.yml" }

oh_instagram:
    instaphp:
        config:
            client_id: %instagram_api_client_id%
            client_secret: %instagram_api_client_secret%
            access_token: %instagram_api_access_token% # (optional)

并添加以下参数

instagram_api_client_id: YOUR_API_ID
instagram_api_client_secret: YOUR_API_SECRET
instagram_api_access_token: YOUR_API_ACCESS_TOKEN # (optional)

大多数 Instaphp 配置密钥可以在 oh_instagram.instaphp.config 中设置。请参阅 配置 Instaphp

如果您接受提供的路由,请将以下内容添加到 app/config/routing.yml

OhInstagramBundle:
    resource: "@OhInstagramBundle/Resources/config/routing.yml"
    prefix:   /

用法(控制器)

$query = "snoopdogg";
	
/**
 * @var InstaphpAdapter
 */
$api = $this->get('instaphp');

$userId = $api->Users->FindId($query);
$media = $api->Users->Recent($userId);

您还可以测试用户是否已登录

//is a user logged in?
$loggedIn = $this->get('instaphp_token_handler')->isLoggedIn();

用法(Twig)

您应该设置您的 Instagram API 账户 以回调到 OhInstagramBundle_callback 路由,您可以为它设置自己的路由,或者使用提供的路由 http://yourserver.com/instaphp/callback

为了快速测试,您可以将以下内容添加到您的路由中

OhInstagramBundle_check:
    path: /checkInstagram
    defaults: { _controller: OhInstagramBundle:Instagram:instagramLoginStatus }

然后导航到 /checkInstagram 以尝试登录按钮。

视图中包含一个登录按钮。只需在您的 Twig 模板中实现此行即可

{{ render(controller('OhInstagramBundle:Instagram:instagramLoginStatus')) }}

Instagram 认证令牌

包含 2 个 TokenHandlers

CookieToken

Instagram 认证代码存储在 cookie 中

services:
    instaphp_token_handler:
        class: Oh\InstagramBundle\TokenHandler\CookieToken

UserToken

Instagram 认证代码存储在用户实体中。必须在您的用户上实现 setInstagramAuthCode()getInstagramAuthCode() 方法。当从 Instagram 返回登录调用时,代码被设置,并在处理器中将用户持久化和刷新。建议您在实体上使用以下接口:Oh\InstagramBundle\TokenHandler\UserTokenInterface

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