<?php
class RPCInterface{
const STATUS_OK = 200;
const STATUS_INVALID_CREDENTIALS = 401;
const STATUS_RESSOURCE_NOT_FOUND = 404;
const STATUS_INTERNAL_ERROR = 500;
const STATUS_NOT_IMPLEMENTED = 501;
const STATUS_SERVICE_UNAVAILABLE = 503;
const ENCAP_RAW = 0;
const ENCAP_AES256 = 1;
const ENCAP_RSA2048 = 2;
protected $encap = 0;
public function getRequestStr($auth, $method, $data){
$req = new stdClass();
$req->auth = $auth;
$req->method = $method;
$req->data = $data;
$reqStr = $this->encode($req);
return $reqStr;
}
public function getResponse($respStr){
$resp = $this->decode($respStr);
return $resp;
}
public function encode($obj){
$str = serialize($obj);// json_encode($str);
$str = $this->encapsulate($str);
return $str;
}
public function decode($str){
$str = $this->decapsulate($str);
$obj = unserialize($str);
return $obj;
}
public function encapsulate($str){
switch($this->encap){
case self::ENCAP_RAW:
$str = base64_encode($str);
break;
case self::ENCAP_AES256:
die('TODO IMPL AES245'); // TODO FIXME
break;
case self::ENCAP_RSA2048:
die('TODO IMPL RSA2048'); // TODO FIXME
break;
default:
die('UNKNOWN_ENCAP_ALG');
}
return $str;
}
public function decapsulate($str){
switch($this->encap){
case self::ENCAP_RAW:
$str = base64_decode($str);
break;
case self::ENCAP_AES256:
die('TODO IMPL AES245'); // TODO FIXME
break;
case self::ENCAP_RSA2048:
die('TODO IMPL RSA2048'); // TODO FIXME
break;
default:
die('UNKNOWN_ENCAP_ALG');
}
return $str;
}
public function responseOk($resp){
return ($resp->status == self::STATUS_OK);
}
public function getError($resp){
return $resp->data['error_num'].' '.$resp->data['error_msg'];
}
public function getErrorDesc($resp){
return $resp->data['error_desc'];
}
public function sendRequest($url, $auth, $method, $data){
$reqStr = $this->getRequestStr($auth, $method, $data);
$request = xmlrpc_encode_request($method, $reqStr); // maybe do not use xmlrpc_ at all...
$context = stream_context_create(array('http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml",
'content' => $request
)));
print_r($context);
//$file = file_get_contents($url, false, $context);
$resp = $this->getResponse($file);
if($this->responseOk($resp)){
$resp->error = false;
}else{
$resp->error = true;
}
return $resp;
}
}
$rpc = new RPCInterface();
$auth = array();
$auth['customer_id'] = '4843929';
$auth['customer_pw'] = 'pw0rdRem0tE0815';
$auth['domain_id'] = '429';
$auth['domain_sec'] = 'a98bf32409cd1fd298e';
$method = 'status.ping';
$url = 'www.example.com';
$data = array();
$req = $rpc->getRequestStr($auth, $method, $data);
$dec = $rpc->getResponse($req);
echo $req;
echo "\r\n";
echo print_r($dec, true);
$resp = $rpc->sendRequest($url, $auth, $method, $data);
if($resp->error){
echo "Error occured!\r\n";
}
?>
preferences:
37.92 ms | 402 KiB | 5 Q