<?php
namespace Grada\REST;
use \Grada\REST\Curl;
class Client
{
private $language = '';
private $hashing_key = '';
function __construct($hashing_key, $language='en-US')
{
$this->language = $language;
$this->hashing_key = $hashing_key;
}
public function call($url, $method, $vars, $headers)
{
if (!array_key_exists('app_key', $headers))
$headers['app_key'] = $vars['app_key'];
if (!array_key_exists('user_email', $headers))
$headers['user_email'] = $vars['user_email'];
if (!array_key_exists('auth_token', $headers))
$headers['auth_token'] = '';
$headers['Accept-Language'] = $this->language;
$headers['request_timestamp'] = gmdate('c');
$headers['request_body'] = $vars;
$headers['request_url'] = $url;
$headers['request_method'] = $method;
$headers['api_signature'] = $this->get_signature($headers);
$request_body = $this->encode_post_data($vars);
$headers['request_body'] = $request_body;
$url = $this->encode_query_string($url);
$headers['request_url'] = $url;
$curl = new Curl();
$curl->headers = $headers;
$response = $curl->request(strtoupper($method), $url, $vars);
$rv = array();
$rv['body'] = json_decode($response->body, true);
$rv['headers'] = $response->headers;
$rv['exception'] =$rv['body']['exception'];
$rv['http_status_code'] = $rv['body']['http_status_code'];
$rv['auth_token'] = $rv['body']['auth_token'];
$rv['body'] = $rv['body']['body'];
return $rv;
}
function encode_query_string($url)
{
$pos = strpos($url, '?',0);
if ($pos !== false)
{
$qs = substr($url,$pos+1);
$qs = rawurlencode(urldecode($qs));
$url = substr($url,0, $pos+1) . $qs;
}
return $url;
}
function encode_post_data($vars)
{
$request_body = '';
if ($vars)
{
foreach ($vars AS $key=>$value)
{
if (is_array($value))
$request_body .= $this->urlencode_array($value, $key).'&';
else
$request_body .= $key.'='.$value.'&';
}
$request_body = rtrim($request_body, '&');
}
return $request_body;
}
function get_signature($vars)
{
$key = $this->hashing_key;
$message = $vars['auth_token'] . $vars['app_key'] . $vars['user_email'] . $vars['request_timestamp'] . $this->encode_post_data($vars['request_body']) .$this->encode_query_string($vars['request_url']) . $vars['request_method'];
return base64_encode(hash_hmac('sha512', $message, hash('sha512', $this->hashing_key)));
}
function urlencode_array($var, $varName, $separator = '&')
{
$toImplode = array();
foreach ($var as $key => $value)
{
if (is_array($value))
$toImplode[] = urlencode_array($value, "{$varName}[{$key}]", $separator);
else
$toImplode[] = "{$varName}[{$key}]=".rawurlencode(urldecode($value));
}
return implode($separator, $toImplode);
}
}
?>
- Output for git.master, git.master_jit, rfc.property-hooks
This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches
Archived branches
Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:
38.92 ms | 401 KiB | 8 Q