PHP library and CLI tool for communicating with an API built with Reflect over HTTP or UNIX sockets
Find a file
Victor Westerlund c819bc3da2
feat: add support for Reflect API keys (#2)
* wip: 1682605782

* wip: 1682607489

* wip: 1682607562

* wip: 1682608764

* wip: 1682609355

* wip: 1682609830

* wip: 1682609969

* feat(doc): add API key reference
2023-04-27 20:26:33 +02:00
src/Reflect feat: add support for Reflect API keys (#2) 2023-04-27 20:26:33 +02:00
.gitignore initial commit 2023-01-05 14:17:17 +01:00
client feat: add HTTP client (#1) 2023-01-18 15:52:24 +01:00
composer.json fix: composer namespace 2023-01-18 17:04:51 +01:00
LICENSE initial commit 2023-01-05 14:17:17 +01:00
README.md feat: add support for Reflect API keys (#2) 2023-04-27 20:26:33 +02:00

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 run by itself or UNIX piped into other programs.


Make a request with Client->call(). It will always 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>", "<API key (optional)>");

$client->call("foo", Reflect\Method::GET); // (array) [200, "bar"]
$client->call("foo", Reflect\Method::POST, [
  "foo" => "bar"
]); // (array) [201, "Created"]

// etc..

How to use

Requires PHP 8.1 or newer, and of course a Reflect API endpoint.

  1. Install with composer

    composer require reflect/client
    
  2. Initialize the class

    require_once "/vendor/autoload.php";
    
    $client = new Reflect\Client("<API URL or path to UNIX socket>", "<API key (optional)>");
    
  3. Make API request

    Use the call() method to perform a request

    $client->call("foo?bar=baz", Reflect\Method::GET);
    
    Argument index Type Required Description
    0 String Yes Fully qualified pathname and query params of the endpoint to call
    1 Method|string Yes A supported Reflect HTTP method (eg. Method::GET) or a string represnetation of a supported method (eg. "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 information

    Index Type Description
    0 Int HTTP-equivalent response code (eg. 200 or 404)
    1 String/Array Contains the response body as either a string, or array if the response Content-Type header is application/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 a Reflect API endpoint.

  1. Clone repo

    git clone https://github.com/victorwesterlund/reflect-client-php
    
  2. Run from command line

    // From the root directory of this project
    php client <url_or_socket_file_path> <endpoint> <http_method> [payload]