public
Created

Redu PHP Client with OAuth

  • Download Gist
redu_client.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
<?php
// this script requires the instalation of the Oauth library
// http://br.php.net/manual/en/book.oauth.php
 
$api_url = 'http://www.redu.com.br/api';
$authorize_url = 'http://www.redu.com.br/oauth/authorize';
$request_token_url = 'http://www.redu.com.br/oauth/request_token';
$access_token_url = 'http://www.redu.com.br/oauth/access_token';
 
$consumer_key = 'PUT_YOUR_CONSUMER_KEY_HERE';
$consumer_secret = 'PUT_YOUR_CONSUMER_SECRET_HERE';
 
session_start();
// In state=1 the next request should include an oauth_token.
// If it doesn't go back to 0
if(!isset($_GET['oauth_token']) && $_SESSION['state']==1) {
$_SESSION['state'] = 0;
}
 
try {
$oauth = new OAuth($consumer_key,
$consumer_secret,
OAUTH_SIG_METHOD_HMACSHA1,
OAUTH_AUTH_TYPE_URI);
$oauth->enableDebug();
 
if(!isset($_GET['oauth_token']) && !$_SESSION['state']) {
$request_token_info = $oauth->getRequestToken($request_token_url);
$_SESSION['secret'] = $request_token_info['oauth_token_secret'];
$_SESSION['state'] = 1;
header('Location: '.$authorize_url.'?oauth_token='.$request_token_info['oauth_token']);
exit;
} else if($_SESSION['state']==1) {
$oauth->setToken($_GET['oauth_token'],$_SESSION['secret']);
$access_token_info = $oauth->getAccessToken($access_token_url);
$_SESSION['state'] = 2;
$_SESSION['token'] = $access_token_info['oauth_token'];
$_SESSION['secret'] = $access_token_info['oauth_token_secret'];
}
 
$oauth->setToken($_SESSION['token'],$_SESSION['secret']);
// fetching Redu Environments
$oauth->fetch("$api_url/environments.json");
$json = json_decode($oauth->getLastResponse());
var_dump($json);
} catch(OAuthException $E) {
var_dump($E);
}
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.