private $debug;
private $url;
private $id;
private $notification = false;
public function __construct($url,$debug = false) {
$this->url = $url;
empty($proxy) ? $this->proxy = '' : $this->proxy = $proxy;
empty($debug) ? $this->debug = false : $this->debug = true;
$this->id = 1;
}
public function setRPCNotification($notification) {
empty($notification) ? $this->notification = false : $this->notification = true;
}
public function __call($method,$params) {
if (!is_scalar($method)) { throw new Exception('Method name has no scalar value'); }
if (is_array($params)) { $params = array_values($params);}else{ throw new Exception('Params must be given as array'); }
if ($this->notification) {$currentId = NULL; }else{ $currentId = $this->id;}
$request = array( 'method' => $method, 'params' => $params, 'id' => $currentId );
$request = json_encode($request);
$this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";
$ch = curl_init($this->url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
$response = json_decode(curl_exec($ch),true);
curl_close($ch);
if ($this->debug) { echo nl2br($debug); }
if (!$this->notification) {
if ($response['id'] != $currentId) { return $response; }
if (!is_null($response['error'])) { return $response; }
return $response['result'];
}else{
return true;
}
}
}
Here's an experimental version i've been tinkering with. I'm thinking using
private $host;
private $port;
private $user;
private $pass;
private $id;
private $notification = false;
public function __construct($host,$port,$user,$pass) {
$this->host = $host;
$this->port = $port;
$this->user = $user;
$this->pass = $pass;
$this->id = 1;
}
public function setRPCNotification($notification) {
empty($notification) ? $this->notification = false : $this->notification = true;
}
public function __call($method,$params) {
if (!is_scalar($method)) { throw new Exception('Method name has no scalar value'); }
if (is_array($params)) { $params = array_values($params);}else{ throw new Exception('Params must be given as array'); }
if ($this->notification) {$currentId = NULL; }else{ $currentId = $this->id;}
$request = array( 'method' => $method, 'params' => $params, 'id' => $currentId );
$request = json_encode($request);
$this->debug && $this->debug.='***** Request *****'."\n".$request."\n".'***** End Of request *****'."\n\n";
$fp = fsockopen('tcp://'.$this->user.':'.$this->pass.'@'.$this->host,$this->port,$errno,$errstr, 10);
if(!$fp){
throw new Exception("$errno - $errstr");
}
$content = "POST $request HTTP/1.1"."\r\n".
"Host: ". $this-host .""."\r\n".
"Content-Type: application/application-json"."\r\n".
"Content-Length:".strlen($request)."\r\n"."\r\n";
fwrite($fp,$content);
$response='';
while(!feof($fp)) {
$response.= fread($fp,1024);
}
fclose($fp);
$response = json_decode(preg_replace('!^.*(?:\r?\n){2}(.*)$!s','\\1',$response));
if ($this->debug) { echo nl2br($debug); }
if (!$this->notification) {
if ($response['id'] != $currentId) { return $response; }
if (!is_null($response['error'])) { return $response; }
return $response['result'];
}else{
return true;
}
}
}