PHP code example of igbas90 / youtube-data-api

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

    

igbas90 / youtube-data-api example snippets


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

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

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

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

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

use Igbas90\YoutubeDataApi\YoutubeDataApi;

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

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

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

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

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


use Igbas90\YoutubeDataApi\YoutubeDataApi;

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

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

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

/** @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);

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();

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");

cp define-example.php define.php