* feat: add HTTP client * feat(doc): update readme * fix(doc): missed a rename
2.8 KiB
Reflect API client for PHP
Make requests to an API built using the Reflect API framework over HTTP or UNIX sockets. This program comes with both an extendable/instantiable class that you can integrate with existing PHP code, or as a stand-alone CLI which can be piped into other programs.
Make a request with Client->call()
. It will return the response as an array of length 2.
- The first value is the HTTP-equivalent response code.
- The second value is the response body
$client = new Reflect\Client("<API URL or path to UNIX socket>");
$client->call("foo", Method::GET); // (array) [200, "bar"]
$client->call("foo", Method::POST, [
"foo" => "bar"
]); // (array) [201, "Created"]
// etc..
How to use
Requires PHP 8.1 or newer, and of course an instance of the Reflect socket server running on the same machine.
-
Install with composer
composer require reflect/client
-
Initialize the class
require_once "/vendor/autoload.php"; $client = new Reflect\Client("<API URL or path to UNIX socket>");
-
Make API request
Use the
call()
method to perform a request$client->call("foo?bar=baz", Method::GET);
Argument index Type Required Description 0 String Yes Fully qualified pathname and query params of the endpoint to call 1 Method Yes A supported Reflect HTTP method (eg. Method::GET
)2 Array No An optional indexed, associative, or multidimensional array that will be sent as the request body as Content-Type: application/json
The
call()
function will return an array of length 2 wil the following informationIndex Type Description 0 Int HTTP-equivalent response code (eg. 200
or404
)1 String/Array Contains the response body as either a string, or array if the response Content-Type
header isapplication/json
How to use (CLI)
You can also run this from the command line with
php client <socket_file> <endpoint> <http_method> [payload]
and it will return a serialized JSON array with the same structure as described in the Client->call()
return.
Example
php client "/run/reflect.sock" "foo?bar=biz" "POST" "[\"foo\" => \"bar\"]" # (string) [201, \"Created\"]
Requires PHP CLI 8.1 or greater, and of course an instance of the Reflect socket server running on the same machine.
-
Clone repo
git clone https://github.com/victorwesterlund/reflect-client-php
-
Run from command line
cd reflect-client-php php client <socket_file> <endpoint> <http_method> [payload]