PHP code example of ray / oauth-module
1. Go to this page and download the library: Download ray/oauth-module library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
ray / oauth-module example snippets
use Ray \Di \AbstractModule ;
use Ray \OAuthModule \OAuth1Module ;
use Ray \OAuthModule \OAuth1Service ;
class AppModule extends AbstractModule
{
protected function configure ()
{
$this ->install(new OAuth1Module(OAuth1Service::TWITTER, $_ENV['CONSUMER_KEY' ], $_ENV['CONSUMER_SECRET' ], '/oauth/callback' ));
}
}
use Ray \OAuthModule \Inject \TwitterOAuthInject ;
class OAuthController
{
use TwitterOAuthInject ;
public function redirectAction ()
{
$this ->twitterOAuth->authorize();
}
}
use Ray \OAuthModule \Inject \TwitterOAuthInject ;
class OAuthController
{
use TwitterOAuthInject ;
public function callbackAction ()
{
if ($_GET['denied' ]) {
return 'ERROR' ;
}
$token = $this ->twitterOAuth->requestAccessToken($_GET['oauth_token' ], $_GET['oauth_verifier' ]);
$userId = $token->getExtraParams()['user_id' ];
$screenName = $token->getExtraParams()['screen_name' ];
$user = $this ->twitterOAuth->api('get' , 'users/show.json' , ['user_id' => $userId]);
$user = json_decode($user);
$result = 'user_id : ' . $userId .'<br />' ;
$result.= 'screen_name : @' . $screenName . '<br />' ;
$result.= 'name: ' . $user->name;
return $result;
}
}
$ php -S localhost:8080 -t docs/demo/www
<< output >>
user_id: {Your User ID}
screen_name: @{your_screen_name}
name: {Your Name}
$ php -S localhost:8080 -t docs/demo/www
<< output >>
id: {Your ID}
name: {Your Name}
docs/demo/www/oauth1_twitter.php
docs/demo/www/oauth2_facebook.php