PHP code example of the-horhe / common-api-client

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

    

the-horhe / common-api-client example snippets



namespace App\Api\Cats\Method;

use Psr\Http\Message\ResponseInterface;
use TheHorhe\ApiClient\AbstractApiMethod;
use TheHorhe\ApiClient\MethodInterface;

class GetImages extends AbstractApiMethod
{
    protected $resultsPerPage = 10;

    /**
     * GetImages constructor.
     * @param int $resultsPerPage
     */
    public function __construct(int $resultsPerPage)
    {
        $this->resultsPerPage = $resultsPerPage;
    }

    public function getQueryParameters()
    {
        return [
            'format' => 'xml',
            'results_per_page' => $this->resultsPerPage
        ];
    }

    public function getHttpMethod()
    {
        return 'GET';
    }

    public function getScheme()
    {
        return 'https';
    }

    public function getHost()
    {
        return 'thecatapi.com';
    }

    public function getMethodUrl()
    {
        return '/api/images/get';
    }

    public function processResponse(ResponseInterface $response)
    {
        return $response->getBody()->getContents();
    }

    public function handleException(\Throwable $exception)
    {
        throw new \Exception($exception->getMessage(), $exception->getCode(), $exception);
    }