EvaOAuth
EvaOAuth provides a standard interface for OAuth1.0 / OAuth2.0 client authorization, it is easy to integrate with any PHP project by very few lines code.
中文文档
Features
Standard interface, same code for both OAuth1.0 and OAuth2.0 different workflow, receiving token and user info as same format either. Fully tested Easy to debug, enable debug mode will record every request and response, help you find out problems quickly. Out-of-the-box, already supported most popular websites including Facebook. Twitter, etc. Scalable, integrate a new oauth website just need 3 lines code.Quick Start
EvaOAuth can be found on Packagist. The recommended way to install this is through composer.
Edit your composer.json and add:
{ "require": { "evaengine/eva-oauth": "~1.0" } }
And install dependencies:
curl -sS https://getcomposer.org/installer | php php composer.phar install
Let's start a example of Facebook Login, if you have already have a Facebook developer account and created an app, prepare a request.php as below:
$service = new EvaEvaOAuthService('Facebook', [ 'key' => 'You Facebook App ID', 'secret' => 'You Facebook App Secret', 'callback' => 'http://localhost/EvaOAuth/example/access.php' ]); $service->requestAuthorize();
Run request.php in browser, will be redirected to Facebook authorization page. After user confirm authorization, prepare the access.php for callback:
$token = $service->getAccessToken();
Once access token received, we could use access token to visit any protected resources.
$httpClient = new EvaEvaOAuthAuthorizedHttpClient($token); $response = $httpClient->get('https://graph.facebook.com/me');
That's it, more usages please check examples and wiki.
Providers
EvaOAuth supports most popular OAuth services as below:
OAuth2.0 Douban Facebook Tencent Weibo OAuth1.0 TwitterCreating a custom provider require only few lines code, for OAuth2 sites:
namespace YourNamespace; class Foursquare extends EvaEvaOAuthOAuth2ProvidersAbstractProvider { protected $authorizeUrl = 'https://foursquare.com/oauth2/authorize'; protected $accessTokenUrl = 'https://foursquare.com/oauth2/access_token'; }
Then register to service and create instance:
use EvaEvaOAuthService; Service::registerProvider('foursquare', 'YourNamespaceFoursquare'); $service = new Service('foursquare', [ 'key' => 'Foursquare App ID', 'secret' => 'Foursquare App Secret', 'callback' => 'http://somecallback/' ]);
Storage
In OAuth1.0 workflow, we need to store request token somewhere, and use request token exchange for access token.
EvaOAuth use DoctrineCache as storage layer. If no configuration, default storage layer use file system to save data, default path is EvaOAuth/tmp.
Feel free to change file storage path before Service
start:
Service::setStorage(new DoctrineCommonCacheFilesystemCache('/tmp'));
Or use other storage such as Memcache:
$storage = new DoctrineCommonCacheMemcacheCache(); $storage->setMemcache(new Memcache()); Service::setStorage($storage);
Events Support
EvaOAuth defined some events for easier injection which are:
BeforeGetRequestToken: Triggered before get request token. BeforeAuthorize: Triggered before redirect to authorize page. BeforeGetAccessToken: Triggered before get access token.For example, if we want to send an additional header before get access token:
$service->getEmitter()->on('beforeGetAccessToken', function(EvaEvaOAuthEventsBeforeGetAccessToken $event) { $event->getRequest()->addHeader('foo', 'bar'); });
Implementation Specification
EvaOAuth based on amazing http client library Guzzle, use fully OOP to describe OAuth specification.
Refer wiki for details:
OAuth1.0 OAuth2.0Debug and Logging
Enable debug mode will log all requests & responses.
$service->debug('/tmp/access.log');
Make sure PHP script have permission to write log path.
API References
Run phpdoc
will generate API references under docs/
.
Join My Telegram Group
版权声明:
1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。