PHP code example of sidus / api-client-bundle

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

    

sidus / api-client-bundle example snippets


/** @var \Sidus\ApiClientBundle\ApiClient $apiClient */

$apiRequest = new \Sidus\ApiClientBundle\Model\Request\AuthenticatedApiRequest(
    new \Sidus\ApiClientBundle\Model\Request\Component\HttpComponent(
        baseUri: 'https://api.example.com',
        path: '/api/v1/resource',
    ),
);

$response = $apiClient->query($apiRequest);

$response->getBody(); // Raw response content

/** @var \Sidus\ApiClientBundle\ApiClient $apiClient */

$apiRequest = new \Sidus\ApiClientBundle\Model\Request\AuthenticatedApiRequest(
    new \Sidus\ApiClientBundle\Model\Request\Component\HttpComponent(
        baseUri: 'https://api.example.com',
        path: '/api/v1/resource',
        method: 'POST',
    ),
);

$apiRequest->setSerializationComponent(
    new \Sidus\ApiClientBundle\Model\Request\Component\SerializationComponent(
        content: new Resource(),
    ),
);
$apiRequest->setDeserializationComponent(
    new \Sidus\ApiClientBundle\Model\Request\Component\DeserializationComponent(
        className: ResourceResponse::class,
    ),
);

$response = $apiClient->query($apiRequest);

$responseResource = $response->getContent(); // Deserialized response content

/**
 * @var \Sidus\ApiClientBundle\ApiClient $apiClient
 * @var \Sidus\ApiClientBundle\Model\Request\ApiRequest $apiRequest
 */
 
$apiRequest->setCacheComponent(
    new \Sidus\ApiClientBundle\Model\Request\Component\CacheComponent(
        ttl: 3600,
    ),
);

$apiRequest = new \Sidus\ApiClientBundle\Model\Request\AuthenticatedApiRequest(
    new \Sidus\ApiClientBundle\Model\Request\Component\HttpComponent(
        baseUri: 'https://api.example.com',
        path: '/api/v1/resource',
    ),
);
$apiRequest->setAuthorizationComponent(
    new Sidus\ApiClientBundle\Model\Authorization\OAuthToken(
        accessToken: 'xxxx',
    ),
);

$apiRequest->addHeader('X-Custom-Header', 'value');