-
-
Save NESS-Network/53f22e2487f342c3515b083aaa06aab5 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
<?php | |
class StorageRequest | |
{ | |
protected $data; | |
protected $error; | |
function __construct($dsn, $method, $params = []) | |
{ | |
if (!(is_array($params) || is_string($params))) { | |
throw new \InvalidArgumentException( | |
sprintf("Invalid parameter type. Must be array or string. Type %s given.", gettype($params)) | |
); | |
} | |
$connect = $dsn; | |
$params = is_string($params) ? [$params] : $params; | |
$request = json_encode( | |
[ | |
'method' => $method, | |
'params' => $params, | |
], | |
JSON_UNESCAPED_UNICODE | |
); | |
$opts = [ | |
'http' => [ | |
'method' => 'POST', | |
'header' => join( | |
"\r\n", | |
[ | |
'Content-Type: application/json; charset=utf-8', | |
'Accept-Charset: utf-8;q=0.7,*;q=0.7', | |
] | |
), | |
'content' => $request, | |
'ignore_errors' => true, | |
'timeout' => 10, | |
], | |
'ssl' => [ | |
"verify_peer" => false, | |
"verify_peer_name" => false, | |
], | |
]; | |
$response = @file_get_contents($connect, false, stream_context_create($opts)); | |
if (!$response) { | |
throw new \ErrorException('Something went wrong. Please wait for a while and try again.'); | |
} | |
$rc = json_decode($response, true); | |
$this->error = $rc['error']; | |
if (!is_null($this->error)) { | |
throw new \ErrorException('EmercoinResponse error: '.$this->error['message']); | |
} | |
$this->data = $rc['result']; | |
} | |
function getData() | |
{ | |
return $this->data; | |
} | |
} | |
try { | |
$request = new StorageRequest('https://emercoin:pa$$w0rd@127.0.0.1:6662', 'getinfo'); | |
// $request = new StorageRequest('https://emercoin:pa$$w0rd@127.0.0.1:6662', 'name_show', 'ssl:here_some_serial'); | |
// $request = new StorageRequest('https://emercoin:pa$$w0rd@127.0.0.1:6662', 'name_show', ['ssl:here_some_serial']); | |
print "Here is data from blockchain below: \n"; | |
var_dump($request->getData()); | |
print "\n"; | |
} catch (\Exception $exc) { | |
print $exc->getMessage()."\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment