private function doCall($url, array $parameters = null, $method = 'GET', $filePath = null, $expectJSON = true, $isContent = false)
{
// allowed methods
$allowedMethods = array('GET', 'POST');
// redefine
$url = (string) $url;
$parameters = (array) $parameters;
$method = (string) $method;
$expectJSON = (bool) $expectJSON;
// validate method
if (!in_array($method, $allowedMethods)) {
throw new Exception('Unknown method (' . $method . '). Allowed methods are: ' . implode(', ', $allowedMethods));
}
// append default parameters
$oauth['oauth_consumer_key'] = $this->getApplicationKey();
$oauth['oauth_token'] = $this->getOAuthToken();
$oauth['oauth_signature_method'] = 'PLAINTEXT';
$oauth['oauth_version'] = '1.0';
$oauth['oauth_signature'] = $this->getApplicationSecret() . '&' . $this->getOAuthTokenSecret();
if ($isContent) {
$headers[] = $this->calculateHeader($oauth, self::API_CONTENT_URL . '/' . $url);
} else {
$headers[] = $this->calculateHeader($oauth, self::API_URL . '/' . $url);
}
$headers[] = 'Expect:';
// set data
$data = $oauth;
if (!empty($parameters)) {
$data = array_merge($data, $parameters);
}
if ($filePath != null) {
// process file
$fileInfo = pathinfo($filePath);
// add to the data
$data['file'] = $fileInfo['basename'];
}
// based on the method, we should handle the parameters in a different way
if ($method == 'POST') {
// file provided?
if ($filePath != null) {
// build a boundary
$boundary = md5(time());
// init var
$content = '--' . $boundary . "\r\n";
// set file
$content .= 'Content-Disposition: form-data; name=file; filename="' . rawurldecode($fileInfo['basename']) . '"' . "\r\n";
$content .= 'Content-Type: application/octet-stream' . "\r\n";
$content .= "\r\n";
$content .= file_get_contents($filePath);
$content .= "\r\n";
$content .= "--" . $boundary . '--';
// build headers
$headers[] = 'Content-Type: multipart/form-data; boundary=' . $boundary;
$headers[] = 'Content-Length: ' . strlen($content);
// set content
$options[CURLOPT_POSTFIELDS] = $content;
} else {
$options[CURLOPT_POSTFIELDS] = $this->buildQuery($parameters);
}
// enable post
$options[CURLOPT_POST] = 1;
} else {
// add the parameters into the querystring
if (!empty($parameters)) {
$url .= '?' . $this->buildQuery($parameters);
}
$options[CURLOPT_POST] = false;
}
// set options
if ($isContent) {
$options[CURLOPT_URL] = self::API_CONTENT_URL . '/' . $url;
} else {
$options[CURLOPT_URL] = self::API_URL . '/' . $url;
}
$options[CURLOPT_PORT] = self::API_PORT;
$options[CURLOPT_USERAGENT] = $this->getUserAgent();
if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) {
$options[CURLOPT_FOLLOWLOCATION] = true;
}
$options[CURLOPT_RETURNTRANSFER] = true;
$options[CURLOPT_TIMEOUT] = (int) $this->getTimeOut();
$options[CURLOPT_SSL_VERIFYPEER] = false;
$options[CURLOPT_SSL_VERIFYHOST] = false;
$options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1;
$options[CURLOPT_HTTPHEADER] = $headers;
// init
if ($this->curl == null) {
$this->curl = curl_init();
}
// set options
curl_setopt_array($this->curl, $options);
// execute
$response = curl_exec($this->curl);
$headers = curl_getinfo($this->curl);
// fetch errors
$errorNumber = curl_errno($this->curl);
$errorMessage = curl_error($this->curl);
if (!$expectJSON && $isContent) {
// is it JSON?
$json = @json_decode($response, true);
if ($json !== false && isset($json['error'])) {
throw new Exception($json['error']);
}
// set return
$return['content_type'] = $headers['content_type'];
$return['data'] = base64_encode($response);
// return
return $return;
}
// we don't expect JSON, return the response
if (!$expectJSON) {
return $response;
}
// replace ids with their string values, added because of some PHP-version can't handle these large values
$response = preg_replace('/id":(\\d+)/', 'id":"\\1"', $response);
// we expect JSON, so decode it
$json = @json_decode($response, true);
// validate JSON
if ($json === null) {
// should we provide debug information
if (self::DEBUG) {
// make it output proper
echo '<pre>';
// dump the header-information
var_dump($headers);
// dump the error
var_dump($errorMessage);
// dump the raw response
var_dump($response);
// end proper format
echo '</pre>';
}
// throw exception
throw new Exception('Invalid response.');
}
// any error
if (isset($json['error'])) {
// should we provide debug information
if (self::DEBUG) {
// make it output proper
echo '<pre>';
// dump the header-information
var_dump($headers);
// dump the raw response
var_dump($response);
// end proper format
echo '</pre>';
}
if (isset($json['error']) && is_string($json['error'])) {
$message = $json['error'];
} elseif (isset($json['error']['hash']) && $json['error']['hash'] != '') {
$message = (string) $json['error']['hash'];
} else {
$message = 'Invalid response.';
}
// throw exception
throw new Exception($message);
}
// return
return $json;
}