lzakrzewski/facebook-authentication-adapter

与 Facebook GRAPH API 通信的适配器

1.0.7 2016-02-07 15:38 UTC

This package is not auto-updated.

Last update: 2024-09-20 18:12:11 UTC


README

Build Status Latest Stable Version Total Downloads

与 Facebook GRAPH API 通信的适配器。

FacebookAuthenticationAdapter 是一个用于与 Facebook GRAPH API 通信的简单库。它返回访问令牌和用户节点数组。 了解关于 Facebook API 访问令牌的更多信息

此库是 FacebookAuthenticationBundle 的独立部分。

<?php

namespace Lzakrzewski\FacebookAuthenticationAdapter\Adapter;

interface FacebookApi
{
    const GRAPH_API_ME_URL = 'https://graph.facebook.com/v2.5/me';
    const GRAPH_API_ACCESS_TOKEN_URL = 'https://graph.facebook.com/v2.5/oauth/access_token';

    /**
     * Returns access token during code exchange.
     *
     * @param $code
     *
     * @throws FacebookApiException
     *
     * @return string
     */
    public function accessToken($code);

    /**
     * Returns a single user node as array.
     *
     * @param string $accessToken
     * @param array  $fields
     *
     * @throws FacebookApiException
     *
     * @return array
     */
    public function me($accessToken, array $fields = array());
}

要求

  "require": {
    "php": ">=5.4",
    "guzzlehttp/guzzle": "~5.0"
  }

支持的 Facebook API 版本

  • v2.5

安装

使用 composer 需要此库

composer require lzakrzewski/facebook-authentication-adapter "~1.0"

示例

<?php

require 'vendor/autoload.php';

if (!isset($_GET['code'])) {
    header("Location: https://#/v2.5/dialog/oauth");
}

if (isset($_GET['code'])) {

    $client = new GuzzleHttp\Client();
    $adapter = new Lzakrzewski\FacebookAuthenticationAdapter\Adapter\GuzzleFacebookApi($client, 'http://my.host/login', 123123123123123, 'app-secret');

    $accessToken = $adapter->accessToken($_GET['code']);
    $userNode = $adapter->me($accessToken, array('first_name', 'last_name', 'gender', 'email', 'birthday', 'name'));

    //Your own logic to process facebook user node
}