crawler-api-node/library/api.php

73 lines
1.7 KiB
PHP
Raw Normal View History

2021-08-07 10:43:53 +00:00
<?php
class API {
private $_url = 'https://explorer.kevacoin.org/api/';
2022-07-16 09:14:46 +00:00
private function _file_get_contents($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3000);
curl_setopt($ch, CURLOPT_TIMEOUT, 10000);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
2021-08-07 10:43:53 +00:00
public function getblockcount() {
// API return: int
2022-07-16 09:14:46 +00:00
if (false !== $response = $this->_file_get_contents($this->_url . 'getblockcount')) {
2021-08-07 10:43:53 +00:00
return (int) $response;
}
return false;
}
public function getblockhash($block) {
2022-07-16 09:14:46 +00:00
if (false !== $response = $this->_file_get_contents($this->_url . 'getblockhash?index=' . $block)) {
2021-08-07 10:43:53 +00:00
// API return: string
if (false !== json_decode($response)) {
return str_replace('"', '', $response);
}
}
return false;
}
public function getblock($hash) {
// API return: json
2022-07-16 09:14:46 +00:00
if (false !== $response = json_decode($this->_file_get_contents($this->_url . 'getblock?hash=' . $hash), true)) {
2021-08-07 10:43:53 +00:00
if (isset($response['hash'])) {
return $response;
}
}
return false;
}
public function getrawtransaction($txid) {
2022-07-16 09:14:46 +00:00
if (false !== $response = json_decode($this->_file_get_contents($this->_url . 'getrawtransaction?txid=' . $txid . '&decrypt=1'), true)) {
2021-08-07 10:43:53 +00:00
if (isset($response['txid'])) {
return $response;
}
}
return false;
}
}