mirror of
https://codeberg.org/reflect/reflect-client-php.git
synced 2025-09-14 10:03:41 +02:00
Compare commits
6 commits
Author | SHA1 | Date | |
---|---|---|---|
89a8c04104 | |||
1ed38a8c3d | |||
6e35a3f7d8 | |||
b029c874df | |||
0110ac1c04 | |||
b33a0e4979 |
2 changed files with 70 additions and 30 deletions
16
README.md
16
README.md
|
@ -137,3 +137,19 @@ Example:
|
||||||
```php
|
```php
|
||||||
$client->call("my/endpoint")->params(["foo" => "bar"])->delete();
|
$client->call("my/endpoint")->params(["foo" => "bar"])->delete();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### `OPTIONS` Request
|
||||||
|
|
||||||
|
Make an `OPTIONS` request by chaining `options()` at the end of a method chain. This method will return a `Reflect\Response` object.
|
||||||
|
|
||||||
|
Use this method to query Reflect for available request methods.
|
||||||
|
|
||||||
|
```php
|
||||||
|
Client->options(): Reflect\Response;
|
||||||
|
```
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```php
|
||||||
|
$client->call("my/endpoint"))->options();
|
||||||
|
```
|
||||||
|
|
|
@ -9,19 +9,21 @@
|
||||||
require_once "Response.php";
|
require_once "Response.php";
|
||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
|
private string $params;
|
||||||
|
private string $endpoint;
|
||||||
|
private array $headers = [];
|
||||||
|
private ?array $payload = null;
|
||||||
|
|
||||||
protected ?string $key;
|
protected ?string $key;
|
||||||
protected string $base_url;
|
protected string $base_url;
|
||||||
protected bool $https_verify_peer;
|
protected bool $https_verify_peer;
|
||||||
|
|
||||||
protected string $endpoint;
|
|
||||||
protected string $params;
|
|
||||||
|
|
||||||
public function __construct(string $base_url, string $key = null, bool $verify_peer = true) {
|
public function __construct(string $base_url, string $key = null, bool $verify_peer = true) {
|
||||||
// Optional API key
|
// Set optional API key and Authorization header
|
||||||
$this->key = $key;
|
$this->key = $key;
|
||||||
|
|
||||||
// Append tailing "/" if absent
|
// Append tailing "/" if absent
|
||||||
$this->base_url = substr($this->base_url, -1) === "/" ? $this->base_url : $this->base_url . "/";
|
$this->base_url = substr($base_url, -1) === "/" ? $base_url : $base_url . "/";
|
||||||
// Flag which enables or disables SSL peer validation (for self-signed certificates)
|
// Flag which enables or disables SSL peer validation (for self-signed certificates)
|
||||||
$this->https_verify_peer = $verify_peer;
|
$this->https_verify_peer = $verify_peer;
|
||||||
}
|
}
|
||||||
|
@ -34,27 +36,25 @@
|
||||||
// ----
|
// ----
|
||||||
|
|
||||||
// Construct stream_context_create() compatible header string
|
// Construct stream_context_create() compatible header string
|
||||||
private function http_headers(): string {
|
private function get_headers(): string {
|
||||||
$headers = ["Content-Type: application/json"];
|
// Set Authorization header if an API key is used
|
||||||
|
if ($this->key) {
|
||||||
// Append Authentication header if API key is provided
|
$this->headers["Authorization"] = "Bearer {$this->key}";
|
||||||
if (!empty($this->key)) {
|
|
||||||
$headers[] = "Authorization: Bearer {$this->key}";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Append new line chars to each header
|
// Construct HTTP headers string from array
|
||||||
$headers = array_map(fn($header): string => $header . "\r\n", $headers);
|
$headers = array_map(fn(string $k, string $v): string => "{$k}: {$v}\r\n", array_keys($this->headers), array_values($this->headers));
|
||||||
return implode("", $headers);
|
return implode("", $headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make request and return response over HTTP
|
// Make request and return response over HTTP
|
||||||
private function http_call(Method $method, array $payload = null): array {
|
private function http_call(Method $method): array {
|
||||||
$context = stream_context_create([
|
$context = stream_context_create([
|
||||||
"http" => [
|
"http" => [
|
||||||
"header" => $this->http_headers(),
|
"header" => $this->get_headers(),
|
||||||
"method" => $method->name,
|
"method" => $method->name,
|
||||||
"ignore_errors" => true,
|
"ignore_errors" => true,
|
||||||
"content" => !empty($payload) ? json_encode($payload) : ""
|
"content" => !empty($this->payload) ? json_encode($this->payload) : ""
|
||||||
],
|
],
|
||||||
"ssl" => [
|
"ssl" => [
|
||||||
"verify_peer" => $this->https_verify_peer,
|
"verify_peer" => $this->https_verify_peer,
|
||||||
|
@ -63,7 +63,7 @@
|
||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$resp = file_get_contents($this->base_url . $this->endpoint, false, $context);
|
$resp = file_get_contents(implode("", [$this->base_url, $this->endpoint, $this->params]), false, $context);
|
||||||
|
|
||||||
// Get HTTP response code from $http_response_header which materializes out of thin air after file_get_contents().
|
// Get HTTP response code from $http_response_header which materializes out of thin air after file_get_contents().
|
||||||
// The first header line and second word will contain the status code.
|
// The first header line and second word will contain the status code.
|
||||||
|
@ -73,6 +73,20 @@
|
||||||
return [$resp, $resp_code];
|
return [$resp, $resp_code];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set request body to be JSON-stringified
|
||||||
|
private function set_request_body(?array $payload = null): self {
|
||||||
|
// Unset request body if no payload defined
|
||||||
|
if (empty($payload)) {
|
||||||
|
$this->payload = null;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->headers["Content-Type"] = "application/json";
|
||||||
|
|
||||||
|
$this->payload = $payload;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
// ----
|
// ----
|
||||||
|
|
||||||
// Construct URL search parameters from array if set
|
// Construct URL search parameters from array if set
|
||||||
|
@ -84,12 +98,14 @@
|
||||||
// Create a new call to an endpoint
|
// Create a new call to an endpoint
|
||||||
public function call(string $endpoint): self {
|
public function call(string $endpoint): self {
|
||||||
// Remove leading "/" if present, as it's already present in $this->base_url
|
// Remove leading "/" if present, as it's already present in $this->base_url
|
||||||
$this->endpoint = substr($this->endpoint, 0, 1) !== "/"
|
$this->endpoint = substr($endpoint, 0, 1) !== "/"
|
||||||
? $this->endpoint
|
? $endpoint
|
||||||
: substr($this->endpoint, 1, strlen($this->endpoint) - 1);
|
: substr($endpoint, 1, strlen($endpoint) - 1);
|
||||||
|
|
||||||
// Reset search parameters
|
// Reset initial values
|
||||||
$this->params();
|
$this->params();
|
||||||
|
$this->headers = [];
|
||||||
|
$this->set_request_body();
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
@ -101,19 +117,27 @@
|
||||||
return new Response(...$this->http_call(Method::GET));
|
return new Response(...$this->http_call(Method::GET));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function patch(array $payload): Response {
|
public function patch(?array $payload = []): Response {
|
||||||
return new Response(...$this->http_call(Method::PATCH, $payload));
|
$this->set_request_body($payload);
|
||||||
|
return new Response(...$this->http_call(Method::PATCH));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function put(array $payload): Response {
|
public function put(?array $payload = []): Response {
|
||||||
return new Response(...$this->http_call(Method::PUT, $payload));
|
$this->set_request_body($payload);
|
||||||
|
return new Response(...$this->http_call(Method::PUT));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function post(array $payload): Response {
|
public function post(?array $payload = []): Response {
|
||||||
return new Response(...$this->http_call(Method::POST, $payload));
|
$this->set_request_body($payload);
|
||||||
|
return new Response(...$this->http_call(Method::POST));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function delete(?array $payload = []): Response {
|
public function delete(?array $payload = []): Response {
|
||||||
return new Response(...$this->http_call(Method::DELETE, $payload));
|
$this->set_request_body($payload);
|
||||||
|
return new Response(...$this->http_call(Method::DELETE));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function options(): Response {
|
||||||
|
return new Response(...$this->http_call(Method::OPTIONS));
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue