PHP code example of nsrosenqvist / php-api-toolkit
1. Go to this page and download the library: Download nsrosenqvist/php-api-toolkit library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nsrosenqvist / php-api-toolkit example snippets
namespace MyApp\Api;
use NSRosenqvist\ApiToolkit\StandardApi;
class Example extends StandardApi
{
public function __construct()
{
parent::__construct([
'base_uri' => 'https://example.com',
'http_errors' => false,
], [
'cache' => true,
'retries' => 3,
'auth' => [
getenv('EXAMPLE_USER'),
getenv('EXAMPLE_PASS')
],
]);
}
// ...
}
use Psr\Http\Message\ResponseInterface;
use NSRosenqvist\ApiToolkit\Structures\Result;
use NSRosenqvist\ApiToolkit\Structures\ListData;
use NSRosenqvist\ApiToolkit\Structures\ObjectData;
// ...
public function resultHandler(ResponseInterface $response, array $options): Result
{
$type = $this->contentType($response);
$data = $this->decode($response->getBody(), $type);
if (is_array($data)) {
$data = new ListData($data);
} elseif (is_object($data)) {
$data = new ObjectData($data);
}
return new Result($response, $data);
}
public function errorHandler(RequestException $reason, array $options): RequestException
{
$this->logError($reason->getMessage());
return $reason;
}
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use NSRosenqvist\ApiToolkit\Structures\Result;
// ...
public function resultHandler(ResponseInterface $response, array $options): Result
{
$result = new Result($response);
if ($result->failure()) {
$request = $response->getRequest();
$exception = RequestException::create($request, $response);
$this->errorHandler($exception);
} else {
// ...
}
return $result;
}
$manager = new NSRosenqvist\ApiToolkit\Manager();
$manager->registerDriver('example', '\\MyApp\\Api\\Example');
$example = $manager->get('example'); // instance of \MyApp\Api\Example
$api = $manager->get('example');
$result = $api->customers->get(); // GET /customers
$result = $api->customers->{100}->get(); // GET /customers/100
// The chain object is immutable, so one could even store a specific endpoint
// and make later chained requests based off of it
$settings = $api->settings; // Instance of \NSRosenqvist\ApiToolkit\RequestChain
$bar = $settings->foo->get();
$ipsum = $settings->lorem->get();
// Async calls works just as well
$promise = $api->customers->getAsync();
// There are also helper functions, these three all perform the same request
$deals = $api->deals->get([
'query' => ['sort' => 'desc'],
]);
$deals = $api->deals->option('query', ['sort' => 'desc'])->get();
$deals = $api->deals->query(['sort' => 'desc'])->get();
$deals = $api->deals->queryArg('sort', 'desc')->get();
// ...
public function resolve(RequestChain $chain): string
{
$options = $chain->getOptions();
if (isset($options['api_version'])) {
return 'v' . $options['api_version'] . '/' . $chain->resolve();
} else {
return $chain->resolve();
}
}
use NSRosenqvist\ApiToolkit\RequestChain;
// ...
public function version($version = '1.5'): RequestChain
{
return $this->chain([
'api_version' => (string) $version,
]);
}
$result = $api->version(2)->customers->get();
$api = $manager->get('httpbin');
$api->mocking(true);
$result = $api->status->{500}->get(); // Will return 200 instead of 500
$api->mocking(false);
$result = $api->status->{500}->get(); // 500 Server Error
$api = $manager->get('httpbin');
$api->queue([
$api->response(200),
$api->response(201),
$api->response(202),
]);
$result = $api->status->{500}->get(); // Will return 200
$result = $api->status->{500}->get(); // Will return 201
$result = $api->status->{500}->get(); // Will return 202
use NSRosenqvist\ApiToolkit\Structures\ObjectData;
$data = new ObjectData([
'foo' => 'bar',
]);
if ($data['foo'] === $data->foo) {
echo 'true';
}
use NSRosenqvist\ApiToolkit\Structures\ObjectData;
ObjectData::macro('html', function() {
return "<p>{$this->foo}</p>";
});
$data = new ObjectData([
'foo' => 'bar',
]);
echo $data->html(); // "<p>bar</p>"
use GuzzleHttp\Psr7\Response;
use NSRosenqvist\ApiToolkit\Structures\Result;
use NSRosenqvist\ApiToolkit\Structures\ListData;
use NSRosenqvist\ApiToolkit\Structures\ObjectData;
// ObjectData
$result = new Result(new Response(/* ... */), new ObjectData([
'foo' => 'bar',
]));
echo $result->foo; // "bar"
// ListData
$result = new Result(new Response(/* ... */), new ListData([
new ObjectData(['foo' => 'bar']),
new ObjectData(['foo' => 'ipsum']),
]));
foreach ($result as $item) {
echo $result->foo;
// First iteration: "bar"
// Second iteration: "ipsum"
}
$api = $manager->get('example');
$customer = $api->cache(true)->customer->{100}->get(); // Fetched from remote
$customer = $api->cache(true)->customer->{100}->get(); // Fetched from cache
$customer = $api->cache(false)->customer->{100}->get(); // Since cache was set to false, it disregarded the cache and fetched it anew from the remote
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.