igbas90/youtube-data-api

Youtube Data API v3

2.0 2020-10-19 09:40 UTC

README

Support APIs

Authorization

!!! This library not supported OAuth2, only GOOGLE CONSOLE API KEY

Installation

Run in console below command to download package to your project:

composer require igbas90/youtube-data-api

Usage

Instance

Using a single shell. Services are created when they are accessed

use Igbas90\YoutubeDataApi\YoutubeDataApi;
$dataApiClient = new YoutubeDataApi();
$commentsThreadClient = $dataApiClient->commentsThreadList;

Create only service

use Igbas90\YoutubeDataApi\Services\CommentsThreadList;
$commentsThreadClient = new CommentsThreadList();

Set params

You can set parameters separately for each service

/** @var $dataApiClient Igbas90\YoutubeDataApi\YoutubeDataApi */
$dataApiClient->commentsThreadList->setApiKey('you google console api key');

$dataApiClient->subscriptionsList->setVideoId('9L9UQANH5oI');

Bulk settings

/** @var $dataApiClient Igbas90\YoutubeDataApi\YoutubeDataApi */
$dataApiClient->commentsThreadList->setParams([
    'apiKey' => "you google console api key",
    'params' => [
        'part' => ['part1', 'part2', ...],
        'videoId' => 'string or array',
        ...
    ]
]);

Parameters such as apiKey, proxy, responseFormatter can be set to services when creating in the shell for this, set these parameters in the shell. After creating the services, you can change these parameters only through the methods of the service itself

use Igbas90\YoutubeDataApi\YoutubeDataApi;

$dataApiClient = new YoutubeDataApi([
    'apiKey' => 'your google console api key',
    'proxy' => 'http://username:password@ip:port',
    'responseFormatter' => new ResponseFormatter()
]);

Usage proxy

To use a proxy server set it to the service

use Igbas90\YoutubeDataApi\YoutubeDataApi;
$dataApiClient = new YoutubeDataApi();

//set proxy
$dataApiClient->commentsThreadList->setProxy('http://username:password@ip:port');

//reset proxy
$dataApiClient->commentsThreadList->resetProxy();

Get result

/** @var $client Igbas90\YoutubeDataApi\Services\CommentsThreadList */
$response = $client->request();

Pagination

For pagination you can manually set pageToken

use Igbas90\YoutubeDataApi\YoutubeDataApi;

$client = (new YoutubeDataApi())->commentsThreadList->setParams([...]);

$response = $client->setPageToken('pageToken')->request();

The package has built-in support for paging data. An ITERATOR is used for this.

!!!Attention, the client pageToken will be used as the start for the iterator

/** @var $client \Igbas90\YoutubeDataApi\Services\CommentsThreadList*/ 
$comments = [];
foreach($client->getIterator() as $response) {
    $body = json_decode($response->getBody(), true);
    $comment = array_merge($comments, $body['items']);
}

By default, the iterator works with the original service, which is not very convenient for using multiple iterators. To exclude the influence of the iterator on the service, you can set the iterator to work with the service clone. To do this, call the getIterator() method, with the parameter true.

/** @var $client \Igbas90\YoutubeDataApi\Services\CommentsThreadList*/ 
$iterator1 = $client->getIterator(true);
$iterator2 = $client->setVideoId('videoId')->getIterator(true);
$iterator3 = $client->setParams([
    'videoId' => '9L9UQANH5oI',
    'apiKey' => 'google console api key'
])->getIterator(true);

Response format

By default, Psr\Http\Message\ResponseInterface is returned If you need to return a different format, then you can specify an object that implements the Igbas90\YoutubeDataApi\Classes\ResponseFormatter interface, which will be used to convert the returned result.

use Igbas90\YoutubeDataApi\Classes\ResponseFormatter;
use Psr\Http\Message\ResponseInterface;

/*
 * Create custom response converter
 */
class BodyJsonDecodeFormatter implements ResponseFormatter
{
    public function format(ResponseInterface $response)
    {
        $content = $response->getBody();
        return json_decode($content, true);
    }
}

/** @var $client Igbas90\YoutubeDataApi\Services\CommentsThreadList */
$client->setResponseFormatter(new BodyJsonDecodeFormatter());

//@var $response array
$response = $client->request();

TEST

forward running the tests you need to set the environment variables in define.php, for this just copy define-example.php into define.php

cp define-example.php define.php

replace define.php values ​​with your values.

define("API_KEY", "google console key");
define("PROXY", "http://username:password@ip:port");
define("CHANNEL_ID", "UCf-b4GSsV5HJysCi6A0Bm6g");
define("PLAYLIST_ID", "UU_x5XG1OV2P6uZZ5FSM9Ttw");
define("VIDEO_ID", "oxQ7wfiS4GY");

Documentations