3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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 5.3.0 - 7.1.0
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/R5T0P on line 2
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/R5T0P on line 2
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/R5T0P on line 2
Process exited with code 255.