Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created August 14, 2014 12:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hubgit/f5dbc97998760cef0cbb to your computer and use it in GitHub Desktop.
Save hubgit/f5dbc97998760cef0cbb to your computer and use it in GitHub Desktop.
PHP OAuth 1.0 client using cURL
<?php
class OAuthClient {
/** @var OAuth */
public $oauth;
/** @var cURL */
public $curl;
function __construct(array $config) {
// sudo pecl install oauth
$this->oauth = new OAuth($config['api_key'], $config['api_secret']);
$this->oauth->setToken($config['access_token'], $config['access_token_secret']);
$this->curl = curl_init();
curl_setopt_array($this->curl, array(
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_VERBOSE => true,
));
}
function exec($url, $data = array(), $method = 'GET') {
curl_setopt($this->curl, CURLOPT_URL, $url);
switch ($method) {
case 'GET':
curl_setopt($this->curl, CURLOPT_HTTPGET, true);
break;
case 'POST':
curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($data));
break;
}
curl_setopt($this->curl, CURLOPT_HTTPHEADER, array(
'Authorization: ' . $this->oauth->getRequestHeader($method, $url, $data)
));
curl_exec($this->curl);
// TODO: retry connection if failed, with backoff
// TODO: retry on idle connection
}
}
@hayden-t
Copy link

hayden-t commented Feb 8, 2019

is there a reason you dont use CURLOPT_RETURNTRANSFER to collect the results of curl_exec and return it to the function using this library ? otherwise thanks i will try this out

@hayden-t
Copy link

hayden-t commented Feb 8, 2019

sudo pecl install oauth
or maybe ?
apt-get install php-oauth

@hayden-t
Copy link

hayden-t commented Feb 8, 2019

I also believe you are incorrect to pass your curl post data into the getRequestHeader function, doing this just gave me incorrect signatures, the only extra parameter i needed was realm

@hayden-t
Copy link

hayden-t commented Apr 8, 2021

ns is switching enforcing HMAC-SHA256 from HMAC-SHA1, so i think this methods need revising to keep working.

$this->oauth = new OAuth($config['api_key'], $config['api_secret'], $signature_method = OAUTH_SIG_METHOD_HMACSHA256);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment