-
-
Save enterstudio/f0f6352a87902fcbda45865b15f797b4 to your computer and use it in GitHub Desktop.
PHP class for the Coinhive HTTP API
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 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