PHP code example of adiafora / php-api-client

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

    

adiafora / php-api-client example snippets


    composer 

 //Example

 $api = new MyApiClient('POST', ['name' => 'Sergey']);
 // or
 $api = new MyApiClient();
 

 $response = $api->send();

    use Adiafora\ApiClient\AbstractApiClient;
    
    class WebmasterApiClient extends AbstractApiClient
    {
        private $resource;    

        private $token;
    
        private $userId;
    
        public function __construct(string $resource, string $method, array $params = [])
        {
            parent::__construct($method, $params);
    
            $this->resource = $resource;
            $this->userId   = USER_ID;
            $this->token    = TOKEN;
        }
    
        public function getHeaders(): array
        {
            return [
                'Authorization: OAuth ' . $this->token,
                'Accept-Language: ru',
                'Content-Type: application/json; charset=utf-8'
            ];
        }
    
        public function getUrl(): string
        {
            return 'https://api.webmaster.yandex.net/v4/user/' . $this->userId . '/' . $this->resource;
        }
    }


// @see https://yandex.com/dev/webmaster/doc/dg/reference/hosts-docpage/
$api = new WebmasterApiClient('hosts', Adiafora\ApiClient\Method::GET);
$response = $api->send();

$response->code(); // 200
$response->response(); // array()

// @see https://yandex.com/dev/webmaster/doc/dg/reference/sqi-history.html/
$api = new WebmasterApiClient('hosts/' . $hostId . '/sqi-history', Adiafora\ApiClient\Method::GET, [
            'date_from' => '2016-01-01T00:00:00,000+0300',
        ]);
$api->send();