PHP code example of los / los-api-client

1. Go to this page and download the library: Download los/los-api-client 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/ */

    

los / los-api-client example snippets


'los_api_client' => [
    'uri' => 'https://localhost:8000',
    'depth' => 0,
    'http_client' => [
        'options' => [
            'timeout'       => 60,
            'sslverifypeer' => false,
            'keepalive'     => true,
            'adapter'       => 'Zend\Http\Client\Adapter\Socket',
        ],
    ],
    'headers' => [
        'Accept'       => 'application/hal+json',
        'Content-Type' => 'application/json',
    ],
]

$httpClient = new Zend\Http\Client('http://127.0.0.1', []);
$client = new LosapiClient\Apt\Client($httpClient, 10);

$httpClient = new Zend\Http\Client('http://127.0.0.1', []);
$storage = Zend\Cache\StorageFactory\StorageFactory::factory([
            'adapter' => [
                'name' => 'memory',
                'options' => [
                    'namespace' => 'my-service',
                ],
            ],
            'plugins' => [
                'exception_handler' => [
                    'throw_exceptions' => false,
                ],
            ],
        ]);
$cerberus = new Cerberus($storage, 2, 2);

// Create a new client
$client = new LosapiClient\Apt\Client($httpClient, 10, $cerberus, 'my-service);

// Or add it to a previously created client 
$client->setCircuitBreaker($cerberus);
$client->setServiceName('my-service');

/* @var \LosApiClient\Api\Client $client */
$client = $this->getServiceLocator()->get('los.api.client');
/* @var \LosApiClient\Resource\Resource $ret */
$ret = $client->get('/album/1');

// $data is an array with all data and resources (_embedded) from the response
$data = $ret->getData();

// $data is an array only with data from the response
$data = $ret->getData(false);

/* @var \LosApiClient\Api\Client $client */
$client = $this->getServiceLocator()->get('los.api.client');

// Setting depth of _embedded resources to 10
$client->setDepth(10);

/* @var \LosApiClient\Resource\Resource $ret */
$ret = $client->get('/album',['year' => 2015]);

// $data is an array with all data and resources (_embedded) from the response
$data = $ret->getData();

// $data is an array with the first album resource from the response
$data = $ret->getFirstResource('album');

// $data is an array with the all album resources from the response
$data = $ret->getResources('album');

// $data is an array with the all resources from the response
$data = $ret->getResources();

/* @var \LosApiClient\Api\Client $client */
$client = $this->getServiceLocator()->get('los.api.client');
/* @var \LosApiClient\Resource\Resource $ret */
$ret = $client->get('/album',['year' => 2015]);

// Returns how many items a page can have
$ret->getPaginator()->getPageSize();

// Returns how many pages the response has
$ret->getPaginator()->getPageCount();

// Returns how many items the response has (across all pages)
$ret->getPaginator()->getTotalItems();

// Returns the current page
$ret->getPaginator()->getPage();

/* @var \LosApiClient\Api\Client $client */
$client = $this->getServiceLocator()->get('los.api.client');
$page = 1;
do {
    /* @var \LosApiClient\Resource\Resource $ret */
    $ret = $client->get('/album',[
        'year' => 2015,
        'page' => $page;
    ]);
    $data = $ret->getData();
    $page++;
} while ($ret->getPaginator()->hasMorePages());