2017 © Pedro Peláez
 

library oauthconsumer

OAuth 1.0 Consumer for PHP

image

danperron/oauthconsumer

OAuth 1.0 Consumer for PHP

  • Friday, August 29, 2014
  • by danperron
  • Repository
  • 1 Watchers
  • 0 Stars
  • 52 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 8 % Grown

The README.md

OAuth 1.0 Consumer for PHP

Installation

Simply add the following requires to your composer.json and run php composer.phar update., (*1)

{
    "minimum-stability":"dev",
    "require": {
        "danperron/oauthconsumer":"*"
    }
}

Usage

Obtaining and authenticating a request token

<?php
/**
 * authenticate.php
 */
require_once './vendor/autoload.php';

use danperron\OAuth\OAuthConsumer;
use danperron\OAuth\OAuthException;

session_start();

$consumerKey = '{YOUR_CONSUMER_KEY}';
$consumerSecret = '{YOUR_CONSUMER_SECRET}';

//create a new OAuthConsumer
$oauthConsumer = new OAuthConsumer($consumerKey, $consumerSecret);

$oauthConsumer->setRequestTokenUrl('https://api.twitter.com/oauth/request_token');
$oauthConsumer->setAccessTokenUrl('https://api.twitter.com/oauth/access_token');
$oauthConsumer->setAuthorizeUrl('https://api.twitter.com/oauth/authorize');
$oauthConsumer->setCallbackUrl('{URL to callback.php}');

try {
    //Fetch request token
    $requestToken = $oauthConsumer->fetchRequestToken();

    //Add the consumer and the request token to the session
    $_SESSION['oauthConsumer'] = $oauthConsumer;
    $_SESSION['requestToken'] = $requestToken;

    //Authorize request token will perform a redirect
    $oauthConsumer->authorizeToken($requestToken);

} catch (OAuthException $e) {
    echo $e->getTraceAsString();
}

Authenticating a request token and making a authorized call.

<?php
/**
 * callback.php
 */
require_once './vendor/autoload.php';

use danperron\OAuth\OAuthConsumer;
use danperron\OAuth\OAuthException;
use danperron\OAuth\OAuthToken;

session_start();

//Retrieve OAuthConsumer from session
$oauthConsumer = $_SESSION['oauthConsumer'];
/* @var $oauthConsumer OAuthConsumer */

//Retrieve request token from session
$requestToken = $_SESSION['requestToken'];
/* @var $requestToken OAuthToken */
try {
    //Trade in newly authorized request token for an access token
    $accessToken = $oauthConsumer->fetchAccessToken($requestToken, array('oauth_verifier' => $_GET['oauth_verifier']));

    //Set the token on the oauth consumer
    $oauthConsumer->setToken($accessToken);

    //Request Twitter timeline and print
    $statuses = $oauthConsumer->makeRequest('https://api.twitter.com/1.1/statuses/home_timeline.json', OAuthConsumer::METHOD_GET);
    echo "Statuses:";
    var_dump(json_decode($statuses));
} catch (OAuthException $e) {
    echo $e->getTraceAsString();
}

The Versions

29/08 2014

dev-master

9999999-dev

OAuth 1.0 Consumer for PHP

  Sources   Download

The Requires

  • php >=5.3.0
  • lib-curl *

 

by Dan Perron