定制 vdmi/guzzle-oauth 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vdmi/guzzle-oauth

Composer 安装命令:

composer require vdmi/guzzle-oauth

包简介

An oAuth 1 and oAuth 2 library based on Guzzle with the big four (google, linkedin, twitter, facebook) baked in.

README 文档

README

Guzzle oAuth is a oAuth 1 and oAuth 2 library based on Guzzle with the big four (google, linkedin, twitter, facebook) baked in.

Installing via Composer

The recommended way to install Guzzle oAuth is through Composer.

# Install Composer
curl -sS https://getcomposer.org/installer | php

Next, update your project's composer.json file to include Guzzle oAuth:

{
    "require": {
        "vdmi/guzzle-oauth": ">=0.0.1"
    }
}

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

Example with other provider

See https://github.com/FransvanderMeer/Guzzle-oAuth-Meetup for an example that uses another provider then one of the big four. The example is an implementation of meetup.com as provider.

Usage

Guzzle oAuth makes the flow to authorize and the user info abstract. That means that you can use the same code for all providers to authorize, get an access token and get the account information.

Send user to provider.

// Authorize flow, send user to provider to authorize
$config = array(
  'consumer_key' => '--YOUR-APP-ID-FROM-FACEBOOK--',
  'consumer_secret' => '--YOUR-APP-SECRET-FROM-FACEBOOK--',
  'scope' => 'email,manage_pages', // scopes for this connection
);
$provider = 'facebook';
$client = \GuzzleOauth\Consumers::get($provider, $config);

// return path after authorization.
$callback_uri = 'http://test.com/callback/uri';

// state param, that makes the round trip to facebook and must be the same on return.
$state = '--some-random-string--'; //optional

// oAuth2 does not use request tokens, we use it here to be consistent with oAuth1
// This makes it possible to replace 'facebook' with 'twitter' in $provider without
// changing the code.
$request_token = $client->getRequestToken($callback_uri);

// get the redirect url
$url = $client->getAuthorizeUrl($request_token, $callback_uri, $state);

// Note!
// You need to store $request_token (and $state) in a session or db, so that you can
// pickit up on return.
$_SESSION['REQUEST_TOKEN_' . $provider] = serialize($request_token);

// send the user to facebook to login and authorize your app.
header('Location: ' . $url);
exit;

On the callback router

// Return (callback) after authorize
// In our example we are on $callback_uri (http://test.com/callback/uri)

// We setup our client again
$config = array(
  'consumer_key' => '--YOUR-APP-ID-FROM-FACEBOOK--',
  'consumer_secret' => '--YOUR-APP-SECRET-FROM-FACEBOOK--',
  'scope' => 'email,manage_pages', // scopes for this connection
);
$provider = 'facebook';
$client = \GuzzleOauth\Consumers::get($provider, $config);

// Let's get the request token
$request_token = unserialize($_SESSION['REQUEST_TOKEN_' . $provider]);

// we could test if $_GET['state'] is the same as the $state from above.

// Let us get the access_token
$access_token = $client->getAccessToken($_GET, $request_token);

// Store access token in session, unset request token
unset($_SESSION['REQUEST_TOKEN_' . $provider]);
$_SESSION['ACCESS_TOKEN_' . $provider] = serialize($access_token);

When we have a valid access token

// Now that we have an access token, do calls to facebook
$provider = 'facebook';

// Get access token
$access_token = unserialize($_SESSION['ACCESS_TOKEN_' . $provider]);

// Add access token to config
$config = array(
  'consumer_key' => '--YOUR-APP-ID-FROM-FACEBOOK--',
  'consumer_secret' => '--YOUR-APP-SECRET-FROM-FACEBOOK--',
  'scope' => 'email,manage_pages', // scopes for this connection
) + $access_token;

// create client
$client = \GuzzleOauth\Consumers::get($provider, $config);

// Get a collection with all user info
$info = $client->getUserInfo();

// Get remote user id without making a new http call
$facebook_id = $client->getUserId($info);

// Get remote user id making a new http call
$facebook_id = $client->getUserId();

// Get email (if provider provides one (note FB needs the scope 'email'))
$email = $client->getUserEmail($info);

// Get user account label (name)
$label = $client->getUserLabel($info);

// $client is a normal guzzle client, so we can talk to any endpoint.
$data = $client->get('me/likes')->send()->json();

vdmi/guzzle-oauth 适用场景与选型建议

vdmi/guzzle-oauth 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 74.85k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2014 年 04 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「rest」 「oauth」 「oauth2」 「Guzzle」 「twitter」 「google」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 vdmi/guzzle-oauth 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 vdmi/guzzle-oauth 我们能提供哪些服务?
定制开发 / 二次开发

基于 vdmi/guzzle-oauth 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 74.85k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 29
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-04-06