Skip to content

Instantly share code, notes, and snippets.

@NESS-Network
Forked from azhuravlov/emercoin_demo.php
Created July 10, 2021 01:24
Show Gist options
  • Save NESS-Network/53f22e2487f342c3515b083aaa06aab5 to your computer and use it in GitHub Desktop.
Save NESS-Network/53f22e2487f342c3515b083aaa06aab5 to your computer and use it in GitHub Desktop.
<?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