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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vdmi/guzzle-oauth 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
WeChat OAuth SDK
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Library for ORCID web services
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
统计信息
- 总下载量: 74.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 29
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-04-06