protected function call($function, array $arguments = array(), $method = 'GET')
{
$arguments['apikey'] = $this->apiKey;
$arguments = array_map(array($this, 'convertBools'), $arguments);
$arguments = http_build_query($arguments, '', '&');
$url = $this->baseUrl . "/" . self::API_VERSION . "/" . $function;
if ($method !== 'POST') {
$url .= "?" . $arguments;
}
// use curl of it's available
if (function_exists('curl_init')) {
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_TIMEOUT, 20);
if ($method === 'POST') {
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $arguments);
}
$result = curl_exec($c);
curl_close($c);
// fallback to plain php
} else {
$params = array('http' => array('method' => $method, 'ignore_errors' => true, 'header' => 'Content-Type:application/x-www-form-urlencoded'));
if ($method === 'POST') {
$params['http']['content'] = $arguments;
}
$context = stream_context_create($params);
$fp = fopen($url, 'rb', false, $context);
$result = $fp ? stream_get_contents($fp) : null;
}
if (!$result) {
throw new \UnexpectedValueException("Empty or No Response from the server");
}
$result = json_decode($result);
if ($result === null) {
throw new \UnexpectedValueException("JSON response could not be decoded");
}
return $this->handleResult($result);
}