fox21/instagram-bundle

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

安装: 7

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 12

类型:symfony-bundle

dev-master 2018-07-01 16:40 UTC

This package is not auto-updated.

Last update: 2024-09-18 04:05:11 UTC


README

使用由 sesser 编写的 Instaphp 作为 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