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.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$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();
useGuzzleHttp\Psr7\Response;
useNSRosenqvist\ApiToolkit\Structures\Result;
useNSRosenqvist\ApiToolkit\Structures\ListData;
useNSRosenqvist\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.