Skip to content

Instantly share code, notes, and snippets.

@enterstudio
Forked from coinhive-com/coinhive-api.php
Created September 22, 2018 17:38
Show Gist options
  • Save enterstudio/f0f6352a87902fcbda45865b15f797b4 to your computer and use it in GitHub Desktop.
Save enterstudio/f0f6352a87902fcbda45865b15f797b4 to your computer and use it in GitHub Desktop.
PHP class for the Coinhive HTTP API
<?php
class CoinHiveAPI {
const API_URL = 'https://api.coinhive.com';
private $secret = null;
public function __construct($secret) {
if (strlen($secret) !== 32) {
throw new Exception('CoinHive - Invalid Secret');
}
$this->secret = $secret;
}
function get($path, $data = []) {
$data['secret'] = $this->secret;
$url = self::API_URL.$path.'?'.http_build_query($data);
$response = file_get_contents($url);
return json_decode($response);
}
function post($path, $data = []) {
$data['secret'] = $this->secret;
$context = stream_context_create([
'http' => [
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
]
]);
$url = SELF::API_URL.$path;
$response = file_get_contents($url, false, $context);
return json_decode($response);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment