diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0d3ddc6..9c12cca 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -47,4 +47,4 @@ jobs: - name: Run test suite run: composer run-script test env: - CASPER_PHP_SDK_TEST_NODE_URL: "65.21.237.160:7777" + CASPER_PHP_SDK_TEST_NODE_URL: "88.99.100.42:7777" diff --git a/README.md b/README.md index 631654a..f9c2fbf 100644 --- a/README.md +++ b/README.md @@ -31,10 +31,12 @@ extension=secp256k1.so ## Usage ### Creating RpcClient -Create `RpcClient` by passing node url to constructor +Create `RpcClient` by passing node url and headers (optional) to constructor ```php $nodeUrl = 'http://127.0.0.1:7777'; -$client = new Casper\Rpc\RpcClient($nodeUrl); +$headers = array('Authorization' => 'Bearer 6ae6c8b31f09df244019ffef60c274e4'); // Optional + +$client = new Casper\Rpc\RpcClient($nodeUrl, $headers); ``` ### RPC call examples diff --git a/docs/API/RpcClientAPI.md b/docs/API/RpcClientAPI.md index 715b5c7..0183165 100644 --- a/docs/API/RpcClientAPI.md +++ b/docs/API/RpcClientAPI.md @@ -4,12 +4,13 @@ Class for interacting with the network via RPC --- ## Constructor ```php -__constructor(string $nodeUrl) +__constructor(string $nodeUrl, array $headers = array()) ``` ### Parameters -| Name | Type | Description | Required | -|---|---|---|---| -| `$nodeUrl` | `string` | Full node url string | Yes | +| Name | Type | Description | Required | +|------------|----------|----------------------|----------| +| `$nodeUrl` | `string` | Full node url string | Yes | +| `$headers` | `array` | Additional headers | No | --- ## Put deploy diff --git a/src/Rpc/RpcClient.php b/src/Rpc/RpcClient.php index 734b4c5..d472785 100644 --- a/src/Rpc/RpcClient.php +++ b/src/Rpc/RpcClient.php @@ -67,11 +67,14 @@ class RpcClient private string $nodeUrl; + private array $headers; + private ?string $lastApiVersion = null; - public function __construct(string $nodeUrl) + public function __construct(string $nodeUrl, array $headers = array()) { $this->nodeUrl = $nodeUrl; + $this->headers = $headers; } public function getLastApiVersion(): ?string @@ -461,20 +464,23 @@ private function rpcCallMethod(string $method, array $params = array()): array { $url = $this->nodeUrl . '/rpc'; $curl = curl_init($url); - $data = array( - 'id' => self::ID, - 'jsonrpc' => self::JSON_RPC, - 'method' => $method, - 'params' => $params - ); + + $headers = ['Accept: application/json', 'Content-type: application/json']; + foreach ($this->headers as $name => $value) { + $headers[] = "$name: $value"; + } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); - curl_setopt($curl, CURLOPT_HTTPHEADER, array( - 'Accept: application/json', - 'Content-type: application/json', + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode( + array( + 'id' => self::ID, + 'jsonrpc' => self::JSON_RPC, + 'method' => $method, + 'params' => $params + ) )); $response = curl_exec($curl);