Skip to content

Instantly share code, notes, and snippets.

@SwimmingTiger
Created March 13, 2017 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SwimmingTiger/04d7a1354b7681a54971e8480dfe89be to your computer and use it in GitHub Desktop.
Save SwimmingTiger/04d7a1354b7681a54971e8480dfe89be to your computer and use it in GitHub Desktop.
bcoin-rpc-proxy.php
<?php
header('Content-Type: application/json');
$url = 'http://localhost:xxxx/';
$auth = 'xxxx';
$userPw = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
if (!hash_equals($auth, $userPw)) {
die('{"error":"Auth failed!"}');
}
$data = file_get_contents('php://input');
$dataArr = json_decode($data);
if (is_object($dataArr) && isset($dataArr->id)) {
$dataArr->id = (int) $dataArr->id;
$data = json_encode($dataArr);
}
$ch = curl_init();
$options = [
CURLOPT_URL => $url,
CURLOPT_USERAGENT => "btc-rpc-test v0.1",
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERPWD => 'btc:' . $auth,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
];
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
if (FALSE === $result) {
echo json_encode(["error" => curl_error($ch)]);
}
else {
$result = json_decode($result);
if (isset($result->result) && isset($result->result->curtime)) {
$result->result->curtime = time();
}
echo json_encode($result);
}
curl_close($ch);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment