arrow_back
Back

YouTube Data API: channels, videos, playlists, and quotas

Andrew Dorokhov Andrew Dorokhov schedule 1 min read
menu_book Table of Contents

open_in_new 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

open_in_new API reference .

Get the list of my broadcasts (open_in_new details ):

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

open_in_new 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"
  }
}
code

Need Help with Development?

Happy to help — reach out via the contacts or go straight to my Upwork profile.

work View Upwork Profile arrow_forward
Next Article

Google API client library setup: Composer, credentials, and first request

arrow_forward