Gigya OAuth class (handles connection and API calls and such for Gigya API calls via OAuth)
<?php
namespace OAuthApp;
class gigya {
// Public
public $api_key = 'some api key';
public $providers = array( 'facebook', 'twitter', 'yahoo', 'messenger', 'googleplus', 'linkedin', 'aol', 'foursquare', 'orkut', 'instagram', 'renren', 'qq', 'sina', 'kaixin', 'vkontakte', 'blogger', 'wordpress', 'typepad', 'paypal', 'amazon', 'livejournal', 'verisign', 'openid', 'netlog', 'signon', 'orangefrance', 'mixi', 'yahoojapan', 'odnoklassniki', 'spiceworks', 'livedoor', 'skyrock', 'vznet', 'xing' );
public $uri;
// Private
private $secret_key = 'some secret key';
private $token_url = 'https://some-token-url.com';
private $dev_url = 'your.devdomain.com';
public function __construct() {
// Change API variables to development connection as needed
if ( strstr( \[site_url](http://codex.wordpress.org/Function_Reference/site_url)(), $this->dev_url ) ) {
$this->api_key = 'change the api key for dev server here';
}
// Initialize
$this->uri = \[get_template_directory_uri](http://codex.wordpress.org/Function_Reference/get_template_directory_uri)() . '/inc/gigya';
}
/**
* Get token from Gigya to retrieve
*
* @param string $code Code returned from client-side socialize.login call
* @param string $redirect_uri Text or HTML to render inside the button
* @return object $response XML response from Gigya containing access_token
*/
public function get_access_token( $code, $redirect_uri ) {
// Setup fields
$auth = 'Authorization: Basic ' . base64_encode( $this->api_key . ':' . $this->secret_key );
$fields = array(
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $redirect_uri,
);
// Build fields string for POST via CURL
// $fields_string = http_build_query( $fields, '', '&' );
$fields_string = 'grant_type=authorization_code&code=' . $code . '&redirect_uri=' . $redirect_uri;
// Post to Gigya with CURL
$curl = curl_init( $this->token_url );
curl_setopt( $curl, CURLOPT_HTTPHEADER, array( $auth, 'Content-Type: application/x-www-form-urlencoded' ) );
curl_setopt( $curl, CURLOPT_HEADER, false );
curl_setopt( $curl, CURLOPT_TIMEOUT, 30 );
curl_setopt( $curl, CURLOPT_POST, count( $fields ) );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $fields_string );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec( $curl );
curl_close( $curl );
// Handle response
$response = json_decode( $response );
return $response;
}
/**
* Send [request](http://codex.wordpress.org/Plugin_API/Filter_Reference/request) to Gigya REST API with access token
*
* @param string $url [request](http://codex.wordpress.org/Plugin_API/Filter_Reference/request) URL for Gigya API call
* @param array $data Associative array of data to be passed
* @param string $access_token Access token obtained from get_access_token()
* @return object $response XML response from Gigya containing access_token
*/
public function send_request( $url, $data, $access_token ) {
// Setup fields
$auth = 'Authorization: OAuth ' . $access_token;
// Build fields string for POST via CURL
$fields_string = http_build_query( $data, '', '&' );
// Post to Gigya with CURL
$curl = curl_init( $url );
curl_setopt( $curl, CURLOPT_HTTPHEADER, array( $auth, 'Content-Type: application/x-www-form-urlencoded' ) );
curl_setopt( $curl, CURLOPT_HEADER, false );
curl_setopt( $curl, CURLOPT_TIMEOUT, 30 );
curl_setopt( $curl, CURLOPT_POST, count( $data ) );
curl_setopt( $curl, CURLOPT_POSTFIELDS, $fields_string );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec( $curl );
curl_close( $curl );
// Handle response
$response = json_decode( $response );
return $response;
}
}