Dorokhov.codes

5. Service: YouTube

Documentation.

Creating a client object

To start working with API lets create a client object:

$client = new Google\Client();

Creating a service object

Make sure we have all the necessary scopes:

use Google\Service\YouTube;

$client->addScope(Youtube::YOUTUBE_READONLY);
$service = new YouTube($client);

Working with broadcasts

API reference.

Get the list of my broadcasts (details):

$service->liveBroadcasts->listLiveBroadcasts('snippet,contentDetails', [
    'broadcastStatus'   => 'active',
    'broadcastType'     => 'all'
]);

API reference.

Request:

https://youtube.googleapis.com/youtube/v3/search
    ?part=snippet
    &key=[YOUR_API_KEY]

PHP:

try {
    $response = $service->search->listSearch('snippet', [
        'channelId' => 'UCMYA1rIPrIcgJXskjIDIM4Q',
        'eventType' => 'live',
        'type'      => 'video',
    ]);

    echo '<pre>';
    var_dump($response->getItems());


} catch (Exception $e) {
    echo 'An error occurred: ' . $e->getMessage();
}

Response:

{
  "kind":"youtube#searchResult",
  "etag":"CvN1I1yZKIPREUctg5gS3LsTCU0",
  "id":{
    "kind":"youtube#video",
    "videoId":"gi_gs8eqD-8"
  },
  "snippet":{
    "publishedAt":"2024-05-16T13:36:35Z",
    "channelId":"UCG9w6mH-s6Kurdjt8KOJnqg",
    "title":"El Infierno también es real | Pastor Jesus Madera",
    "description":"Bienvenidos! Gracias por ser parte del servicio de hoy. Recuerda invitar a alguien a ver el mensaje dando click en compartir.",
    "thumbnails":{
      "default":{
        "url":"https://i.ytimg.com/vi/gi_gs8eqD-8/default.jpg",
        "width":120,
        "height":90
      },
      "medium":{
        "url":"https://i.ytimg.com/vi/gi_gs8eqD-8/mqdefault.jpg",
        "width":320,
        "height":180
      },
      "high":{
        "url":"https://i.ytimg.com/vi/gi_gs8eqD-8/hqdefault.jpg",
        "width":480,
        "height":360
      }
    },
    "channelTitle":"Gosén TV",
    "liveBroadcastContent":"none",
    "publishTime":"2024-05-16T13:36:35Z"
  }
}