Created
September 7, 2015 23:58
-
-
Save delboy1978uk/dcb8b9fb28eb64374ba7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function __construct($username, $password, $host = 'localhost', $port = 80, $url = null) { | |
$this->username = $username; | |
$this->password = $password; | |
$this->host = $host; | |
$this->port = $port; | |
$this->url = $url; | |
// Set some defaults | |
$this->proto = 'http'; | |
$this->CACertificate = null; | |
} | |
/** | |
* @param string|null $certificate | |
*/ | |
function setSSL($certificate = null) { | |
$this->proto = 'https'; // force HTTPS | |
$this->CACertificate = $certificate; | |
} | |
function __call($method, $params) { | |
$this->status = null; | |
$this->error = null; | |
$this->raw_response = null; | |
$this->response = null; | |
// If no parameters are passed, this will be an empty array | |
$params = array_values($params); | |
// The ID should be unique for each call | |
$this->id++; | |
// Build the request, it's ok that params might have any empty array | |
$request = json_encode(array( | |
'method' => $method, | |
'params' => $params, | |
'id' => $this->id | |
)); | |
// Build the cURL session | |
$curl = curl_init("{$this->proto}://{$this->username}:{$this->password}@{$this->host}:{$this->port}/{$this->url}"); | |
$options = array( | |
CURLOPT_RETURNTRANSFER => TRUE, | |
CURLOPT_FOLLOWLOCATION => TRUE, | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_HTTPHEADER => array('Content-type: application/json'), | |
CURLOPT_POST => TRUE, | |
CURLOPT_POSTFIELDS => $request | |
); | |
// This prevents users from getting the following warning when open_basedir is set: | |
// Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set | |
if (ini_get('open_basedir')) { | |
unset($options[CURLOPT_FOLLOWLOCATION]); | |
} | |
if ($this->proto == 'https') { | |
// If the CA Certificate was specified we change CURL to look for it | |
if ($this->CACertificate != null) { | |
$options[CURLOPT_CAINFO] = $this->CACertificate; | |
$options[CURLOPT_CAPATH] = DIRNAME($this->CACertificate); | |
} | |
else { | |
// If not we need to assume the SSL cannot be verified so we set this flag to FALSE to allow the connection | |
$options[CURLOPT_SSL_VERIFYPEER] = FALSE; | |
} | |
} | |
curl_setopt_array($curl, $options); | |
// Execute the request and decode to an array | |
$this->raw_response = curl_exec($curl); | |
$this->response = json_decode($this->raw_response, TRUE); | |
// If the status is not 200, something is wrong | |
$this->status = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
// If there was no error, this will be an empty string | |
$curl_error = curl_error($curl); | |
curl_close($curl); | |
if (!empty($curl_error)) { | |
$this->error = $curl_error; | |
} | |
if ($this->response['error']) { | |
$this->error = $this->response['error']['message']; | |
} | |
elseif ($this->status != 200) { | |
// If bitcoind didn't return a nice error message, we need to make our own | |
switch ($this->status) { | |
case 400: | |
$this->error = 'HTTP_BAD_REQUEST'; | |
break; | |
case 401: | |
$this->error = 'HTTP_UNAUTHORIZED'; | |
break; | |
case 403: | |
$this->error = 'HTTP_FORBIDDEN'; | |
break; | |
case 404: | |
$this->error = 'HTTP_NOT_FOUND'; | |
break; | |
} | |
} | |
if ($this->error) { | |
return FALSE; | |
} | |
return $this->response['result']; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment