Recently i came across a good PHP library for OAuth. It is Lusitanian PHP Oauth Library.
It is a very robust library for implementing three legged OAuth and supports many OAuth service providers such as Google, Facebook, Twitter, etc.
Authorizing a Twitter user is done as follows:
$token = $twitterService->requestRequestToken();
$url = $twitterService->getAuthorizationUri(array('oauth_token' => $token->getRequestToken()));
header('Location: ' . $url);
After the user gets authorized, we get OAuth Token and OAuth Verifier values. Using them, we will fetch an Access Token and make OAuth calls to Twitter as follows:
$result = json_decode($twitterService->request('account/verify_credentials.json'));
echo 'Result: <br>' . print_r($result);
The library primarily uses session to store the access token, after it is retrieved with the successful user login at the Provider. But if you want to use an existing access token (instead of making the user login each time), you can tweak the library to use the existing access token and store it in the session, for using it immediately (Note that the token should be valid and not be expired).
To store an existing access token in session, you can modify: Service/AbstractService.php:
$token = new StdOAuth1Token();
$_SESSION['access_token'] = serialize($token);
Once you have a valid access token in session, you can use it to initiate API calls as follows:
Modify the request() function of Service/AbstractService.php, to use access token from php session:
//$token = $this->storage->retrieveAccessToken($this->service());
$token = unserialize($_SESSION['access_token']);
That’s it. Just download the library via Composer, include it in your PHP application and start using OAuth APIs.