Get to know oauthhelper.php

This class will help users make request to API using OAuth 1.0, for more info on OAuth 1.0 please read this tutorial

<?php

/*
* This file build on OAUTH 1.0a
*/

class oauthhelper {

public static function request($config, $endpoint, $access_token, $method = 'GET', $extend_params = null) {
$params = self::get_request_info_params($config, $access_token);
$oauth_sig = self::build_oauth_sig($params, $method, $endpoint, $config, $access_token['oauth_token_secret']);
$header = self::build_authen_header($config, $params, $oauth_sig, $extend_params, $endpoint, $method);
$data = self::make_request($endpoint, $header, null, $method);

return ($data);
}

/**
*
* @param array $config
* @param string $endpoint
* @param array $access_token
* @param tring $method
* @return array
*/
public static function get_info($config, $endpoint, $access_token, $method = 'GET', $extend_params = null) {
$params = self::get_request_info_params($config, $access_token);
$oauth_sig = self::build_oauth_sig($params, $method, $endpoint, $config, $access_token['oauth_token_secret']);
$header = self::build_authen_header($config, $params, $oauth_sig, $extend_params, $endpoint, $method);
$data = self::make_request($endpoint, $header, null, $method);

return ($data);
}

/**
*
* @param array $config
* @param string $method
* @param string $endpoint
* @param array $temp_token
* @param string $oauth_verifier
* @return array
*/
public static function get_access_token($config, $method, $endpoint, $temp_token, $oauth_verifier) {
$params = self::get_access_token_params($config, $temp_token, $oauth_verifier);
$oauth_sig = self::build_oauth_sig($params, $method, $endpoint, $config, $temp_token['oauth_token_secret']);
$header = self::build_authen_header($config, $params, $oauth_sig, null, $endpoint, $method);

$data = self::make_request($endpoint, $header, null, $method);

return $data;
}

/**
*
* @param array $config
* @param string $method
* @param string $endpoint
* @return array
*/
public static function get_request_token($config, $method, $endpoint) {
$params = self::get_request_token_params($config);
$oauth_sig = self::build_oauth_sig($params, $method, $endpoint, $config);
$header = self::build_authen_header($config, $params, $oauth_sig, null, $endpoint, $method);
var_dump($header);

$data = self::make_request($endpoint, $header, null, $method);
return $data;
}

/**
*
* @param array $params
* @param string $oauth_sig
* @return string
*/
public static function build_authen_header($config, $params, $oauth_sig, $extend_params = null, $endpoint = null, $method = null) {
if ($extend_params != null) {
foreach ($extend_params as $key => $value) {
$params[$key] = $value;
}
}
$ret = array();

$params['oauth_signature'] = $oauth_sig;

ksort($params);

foreach ($params as $parameter => $value) {
$pairs[] = $parameter . '="' . rawurlencode($value) . '"';
}
$params_data = implode(', ', $pairs);

if ($endpoint != null) {
$path_data = self::get_url_info($endpoint);
if (isset($config['url_parameters']) && $config['url_parameters'] == true) {
// var_dump($params_data);
$raw = str_replace('"', '', $params_data);
// var_dump($raw);
$raw = str_replace(', ', '&', $raw);
// var_dump($raw);
$url_params = $path_data['path'] . '?' . $raw . ' HTTP/1.1';
} else {
$url_params = $path_data['path'];
}
$ret[] = $method . ' ' . $url_params;
$ret[] = 'Host: ' . $path_data['host'];
}

$ret[] = 'Accept: */*';
if (isset($config['authorization_header']) && $config['authorization_header'] == false) {

} else {
$authen = 'Authorization: OAuth ' . $params_data;
$ret[] = $authen;
}


$ret[] = 'Expect:';
return $ret;
// return ['Accept: application/json', $authen, 'Expect:'];
}

/**
* get params array for calling request token
* @param array $config
* @return array
*/
private static function get_request_token_params($config) {
$params = array(
'oauth_version' => '1.0',
'oauth_nonce' => self::generateNonce(),
'oauth_timestamp' => time(),
'oauth_consumer_key' => $config['client_id'],
'oauth_callback' => $config['redirect_uri'],
'oauth_signature_method' => 'HMAC-SHA1',
);
return $params;
}

/**
* get params array when calling access token
* @param array $config
* @param array $temp_token
* @param string $oauth_verifier
* @return array
*/
private static function get_access_token_params($config, $temp_token, $oauth_verifier) {
$params = array(
'oauth_version' => '1.0',
'oauth_nonce' => self::generateNonce(),
'oauth_timestamp' => time(),
'oauth_consumer_key' => $config['client_id'],
'oauth_token' => $temp_token['oauth_token'],
'oauth_verifier' => $oauth_verifier,
'oauth_signature_method' => 'HMAC-SHA1',
);

return $params;
}

/**
* get params array when calling request user'd details
* @param array $config
* @param array $access_token
* @return array
*/
private static function get_request_info_params($config, $access_token) {
$params = array(
'oauth_version' => '1.0',
'oauth_nonce' => self::generateNonce(),
'oauth_timestamp' => time(),
'oauth_consumer_key' => $config['client_id'],
'oauth_token' => $access_token['oauth_token'],
'oauth_signature_method' => 'HMAC-SHA1',
);

return $params;
}

/**
*
* @param array $params
* @param string $method
* @param string $endpoint
* @param array $config
* @param string $token_secret
* @return array
*/
private static function build_oauth_sig($params, $method, $endpoint, $config, $token_secret = '') {
$base_string = self::create_base_string($params, $method, $endpoint);
$key = self::create_key($config, $token_secret);
var_dump($key);
var_dump($base_string);
$ret = base64_encode(hash_hmac('sha1', $base_string, $key, true));
var_dump($ret);
return $ret;
}

private static function create_base_string($params, $method, $endpoint) {
uksort($params, 'strcmp');
foreach ($params as $parameter => $value) {
$pairs[] = $parameter . '=' . rawurlencode($value);
}

$params_data = (implode('&', $pairs));
$base_array = [
strtoupper($method),
$endpoint,
$params_data
];
$base_encode = array();
foreach ($base_array as $base) {
$base_encode[] = rawurlencode($base);
}

$base_string = implode('&', $base_encode);
return $base_string;
}

private static function create_key($config, $token_secret = '') {
$parts = [rawurlencode($config['client_secret']), rawurlencode($token_secret)];
$key = implode('&', $parts);
return $key;
}

/**
* return randon string
* @return string
*/
public static function generateNonce() {
return md5(microtime() . mt_rand());
}

/**
* make request data
* @param string $endpoint
* @param array $header
* @param string $post_fields
* @return string
*/
public static function make_request($endpoint, $header = null, $post_fields = null, $method = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);

curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
switch ($method) {
case 'GET':
break;
case 'POST':
break;
default :
break;
}
if ($header != null) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
if ($post_fields != null) {
// curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
}
$data = curl_exec($ch);

if (curl_errno($ch) > 0) {
return parse_string_to_array(curl_error($ch));
}

curl_close($ch);
return self::parse_string_to_array($data);
}

/**
* parse string to array
* @param string $input
* @return array
*/
private static function parse_string_to_array($input) {

if ($input == null && $input == '') {
return '';
} else if ($input != strip_tags($input)) {
// contains HTML
return $input;
} else if (json_decode($input) != null) {
return json_decode($input);
} else if (strpos($input, '&') != false) {
$ret = array();
$value_array = explode('&', $input);
if (count($value_array)) {
foreach ($value_array as $data) {
$key_data = explode('=', $data);
if (count($key_data) > 0) {
$ret[$key_data[0]] = $key_data[1];
}
}
}

return $ret;
} else {
return $input;
}
}

private static function get_url_info($endpoint) {
return (parse_url($endpoint));
}

}

Functions

make_request($endpoint, $header = null, $post_fields = null)

Make request to api, return array, if there is error, print on screen

Example

  • $method = 'GET';
    $endpoint = 'https://api.twitter.com/oauth/request_token';
    $header = array();
    $post_fileds = '';
    $data = self::make_request($endpoint, $header,$post_fileds, $method);

parse_string_to_array($input)

Convert string to array 

Example:

  • $data = 'oauth_token=fzMrQjsje0ecgPYj7DZ6uGwzMs3koY4mCbfwGqJNYeiR7DtCWG&oauth_token_secret=cOdIqDF4oJesKsbquBXGUzH9vHQA9MhXyD3h3N4vproo4S0Vn7&oauth_callback_confirmed=true';
    $ret = projecthelper::convert_string_to_array($data);

generateNonce()

Generate a random string 


create_key($config, $token_secret = '')

Create key for signature


create_base_string($params, $method, $endpoint)

Create basestring


build_oauth_sig($params, $method, $endpoint, $config, $token_secret = '')

Create signature


get_request_info_params($config, $access_token)

Create parameter to get user's profile


get_access_token_params($config, $temp_token, $oauth_verifier)

create paramenter to get access_token 


get_request_token_params($config)

create paramenter to get temporary token


build_authen_header($params, $oauth_sig)

create request header


get_request_token($config, $method, $endpoint)

Get request token 


get_access_token($config, $method, $endpoint, $temp_token, $oauth_verifier)

Get access token


get_info($config, $endpoint, $access_token, $method = 'GET')

Get user's profile

If you have questions, don't hesitate, tell us, or read twitter login tutorial for more usage