DEV Community

Ichi
Ichi

Posted on • Edited on

[PHP] How to easily get video information from YouTube

If you want to get the video information of a specific channel or the playlist list, I think you will use YouTube API v3.
With PHP, it's good to use the official Google library with Composer, but for certain videos you can get the video information more easily.

Specifically, the JSON that stores the video information is called from the API.
that's all!

JSON request parameter structure

https://www.googleapis.com/youtube/v3/videos?id=[videoID]&key=[APIKey]&part=snippet,contentDetails,statistics,status

[TIPS]The video ID is the character string following "watch? V = ~" in the URL.
Alt Text
Enter the API key obtained from Google API in [API Key].

Sample code

Try to get the title of a specific video from YouTube API v3.

$api_key = "API Key"
$video_id = "Video ID"
$url = "https://www.googleapis.com/youtube/v3/videos?id=" . $video_id . "&key=" . $api_key . "&part=snippet,contentDetails,statistics,status";
$json = file_get_contents($url);
$getData = json_decode( $json , true);
foreach((array)$getData['items'] as $key => $gDat){
    $title = $gDat['snippet']['title'];
}
// Output title
echo $title;
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay