PHP Client Example

Example PHP code to form a request to the Bluestore API:

<?php
class OAUTH_ALGORITHMS
{
const RSA_SHA1 = 'RSA-SHA1';
}

define('ACCESS_TOKEN', 'xxxxxxxxxxxxxxxxxxxxxxxxx'); // Change with your Bluestore Access Token value
define('API_KEY', 'xxxxxxxxxxxxxxxxxxxxxxxxxx'); // Change with your Bluestore API Key value
define('PRIVATE_CERTIFICATE_FILE_PATH', 'xxxxxxxxxx'); // Change with complete path of privatekey.pem file
define('API_URL', 'https://bluestorelive.com:9001/rest/sale'); // Define Bluestore API URL here

// For condition base output
$filterCode = "428"; // for a single data output need to change this value
$condition = array(); //'where'=>rawurlencode('code = 10001')
$oauthOutput = get_auth_header(API_URL, API_KEY,ACCESS_TOKEN,PRIVATE_CERTIFICATE_FILE_PATH,$condition);
$API_URL = API_URL;//."?where=".$condition['where'];


// For all category/product/stock data
/*$oauthOutput = get_auth_header(API_URL, API_KEY,ACCESS_TOKEN,PRIVATE_CERTIFICATE_FILE_PATH);
$API_URL = API_URL;*/

$header = array(
'Accept: application/xml',
'Content-Type: application/xml',
'Expect:'
);
$header = array_merge($oauthOutput,$header);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$API_URL);
curl_setopt($ch, CURLOPT_PORT , 9001);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
$CurlResponse = curl_exec($ch);

echo "<pre>";
print_r(curl_error($ch));
print_r($CurlResponse);


function sign_rsa_sha1($method, $baseurl, $certfile, array $parameters)
{
$fp = fopen($certfile, "r");
$private = fread($fp, 8192);
fclose($fp);

$data = $method.'&';
$data .= urlencode($baseurl).'&';
$oauth = '';

ksort($parameters);
foreach($parameters as $key => $value)
$oauth .= "&{$key}={$value}";
$data .= rawurlencode(substr($oauth, 1));
$keyid = openssl_get_privatekey($private);
$sign=openssl_sign($data, $signature, $keyid);

openssl_free_key($keyid);
return base64_encode($signature);
}

function build_auth_array($api_url, $api_key,$access_token,$certificate_file, $extra = array(), $method = 'GET', $algo = OAUTH_ALGORITHMS::RSA_SHA1)
{
$auth['oauth_consumer_key'] = $api_key;
$auth['oauth_signature_method'] = $algo;
$auth['oauth_timestamp'] = time();
$auth['oauth_token'] = $access_token;
$auth['oauth_nonce'] = md5(uniqid(rand(), true));
$auth['oauth_version'] = '1.0';

if(isset($extra) && !empty($extra))
{
$auth = array_merge($extra, $auth);
}

if(strtoupper($algo) == OAUTH_ALGORITHMS::RSA_SHA1)
$auth['oauth_signature'] = sign_rsa_sha1 ($method, $api_url, $certificate_file, $auth);
$auth['oauth_signature'] = urlencode($auth['oauth_signature']);

print_r($auth);

return $auth;
}

function build_auth_string(array $authparams)
{
$header = "Authorization: OAuth ";
foreach($authparams AS $key=>$value)
$header .= ",{$key}=\"{$value}\"";
return array($header);
}

function get_auth_header($api_url, $api_key,$access_token,$certificate_file,$extra = array(), $method = 'GET', $algo = OAUTH_ALGORITHMS::RSA_SHA1)
{
$auth = build_auth_array($api_url, $api_key,$access_token,$certificate_file, $extra, $method, $algo);
return build_auth_string($auth);
}

?>

 

Have more questions? Submit a request

0 Comments

Article is closed for comments.
Powered by Zendesk