This library is an opinionated, resource-based JSON API client for PHP that strives to adhere to the offical JSON API 1.0 spec.
- PHP 5.4 or later
- Guzzle (auto-included if using Composer)
You can install it via Composer. Run the following command:
composer require cloudswipe/json-api-php-client
To use the bindings, use Composer's autoload:
require_once('vendor/autoload.php');
baseUrl
is specified as the root URL used when interacting with the API.url
is the specific part of the URL for the current resourcetype
is the JSON API type for the current resource
use JsonApiClient\Resource;
class Invoice extends Resource
{
public function __construct()
{
parent::__construct(
"https://api.site.com/v1/", // base URL
"invoices" // type
)
}
}
The username
is required, but the password
is optional and defaults to blank.
Resource::auth("jdoe", "secret");
If you're using a typical API key over HTTP Authentication, here is an example of using a base class to abstract that away.
class Base extends Resource
{
public function __construct($type)
{
parent::__construct("http://api.site.come/v1/", $type);
}
public static function setApiKey($apiKey)
{
parent::auth($apiKey);
}
}
class Invoice extends Base
{
public function __construct()
{
parent::__construct("invoices");
}
}
Base::setApiKey("some secret key");
$invoices = Invoice::getAll();
$invoice = Invoice::create([
"description" => "T-Shirt",
"total" => 10.95
]);
Update a resource with partial updates. This uses the PATCH
method under the
hood. See Where's PUT?.
$invoice = Invoice::update("invoice_123", [
"description" => "Blue T-Shirt"
]);
$invoice = Invoice::getOne("invoice_123");
$invoices = Invoice::getAll();
Invoice::delete("invoice_123");