* feat: add support for OPTIONS requests * feat(doc): add OPTIONS ref. to README |
||
|---|---|---|
| src/Reflect | ||
| .gitignore | ||
| composer.json | ||
| LICENSE | ||
| README.md | ||
Reflect API client for PHP
Make requests from a PHP application to an API built with the Reflect API framework.
Installation
- Install with composer
composer require reflect/client
usein your PHP code
use Reflect\Client;
$client = new Client(string $api_base_url, ?string $api_key, ?bool $verify_peer = true);
Making API calls
Start by initializing the Client with a base URL to the API, and with an optional API key.
use Reflect\Client;
use Reflect\Method;
$client = new Client("https://api.example.com", "MyApiKey");
Defining an endpoint
Start a new API call by chaining the call() method and passing it an endpoint string
Client->call(string $endpoint): self
Example:
$client->call("my/endpoint");
(Optional) Search Parameters
Pass an associative array of keys and values to params(), and chain it anywhere before a get(), patch(), put(), post(), or delete() request to set search ($_GET) parameters for the current request.
Client->params(?array $params = null): self
Example:
// https://api.example.com/my/endpoint?key1=value1&key2=value2
$client->call("my/endpoint")
->params([
"key1" => "value1",
"key2" => "value2"
]);
GET Request
Make a GET request by chaining get() at the end of a method chain. This method will return a Reflect\Response object.
Client->get(): Reflect\Response;
Example:
$client->call("my/endpoint")->params(["foo" => "bar"])->get();
POST Request
Make a POST request by chaining post() at the end of a method chain. This method will return a Reflect\Response object.
Pass post() a stringifiable associative array of key, value pairs to be sent as an application/json-encoded request body to the endpoint.
Client->post(array $payload): Reflect\Response;
Example:
$client->call("my/endpoint")->params(["foo" => "bar"])->post(["baz" => "qux"]);
PATCH Request
Make a PATCH request by chaining patch() at the end of a method chain. This method will return a Reflect\Response object.
Pass patch() a stringifiable associative array of key, value pairs to be sent as an application/json-encoded request body to the endpoint.
Client->patch(array $payload): Reflect\Response;
Example:
$client->call("my/endpoint")->params(["foo" => "bar"])->patch(["baz" => "qux"]);
PUT Request
Make a PUT request by chaining put() at the end of a method chain. This method will return a Reflect\Response object.
Pass put() a stringifiable associative array of key, value pairs to be sent as an application/json-encoded request body to the endpoint.
Client->put(array $payload): Reflect\Response;
Example:
$client->call("my/endpoint")->params(["foo" => "bar"])->put(["baz" => "qux"]);
DELETE Request
Make a DELETE request by chaining delete() at the end of a method chain. This method will return a Reflect\Response object.
Pass delete() an optional stringifiable associative array of key, value pairs to be sent as an application/json-encoded request body to the endpoint.
Client->delete(?array $payload = null): Reflect\Response;
Example:
$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.
Client->options(): Reflect\Response;
Example:
$client->call("my/endpoint"))->options();