Get youtube title from videoid in PHP using API v3

3.6k Views Asked by At

Here is the url to to get the video info where you have to put VIDEO-ID and API-KEY:

https://www.googleapis.com/youtube/v3/videos?part=snippet&id=VIDEO-ID-HERE&key=YOUR-API-KEY-HERE

How to get the title from it and save it as a variable in PHP?

2

There are 2 best solutions below

0
On BEST ANSWER

Something along these lines will get you the information related to a specific video using the PHP client library:

<?php

require_once 'Google/autoload.php';
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';

$client = new Google_Client();
$client->setDeveloperKey('{YOUR-API-KEY}');
$youtube = new Google_Service_YouTube($client);

$videoResponse = $youtube->videos->listVideos('snippet', array(
    'id' => '{YOUR-VIDEO-ID}'
));

$title = $videoResponse['items'][0]['snippet']['title'];
?>

<!doctype html>
<html>
  <head>
    <title>Video information</title>
  </head>
  <body>
  Title: <?= $title ?>
  </body>
 </html>

One more solution with API Request

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
        $(document).ready(function() {
        $.get(
            "https://www.googleapis.com/youtube/v3/videos",{
            part : 'snippet', 
            id : 'VIODE_ID',
            key: 'API_KEY'},
            function(data) {
           $.each( data.items, function( i, item ) {
                    alert(item.snippet.title);
               });
           }
         );
}); 
</script>
0
On

There are two PHP examples in google developers.

Also, you can try a test in the bottom of this page. Input the part(snippet) and id(select one youtube id) fields. It will demonstrate to you with GET request and JSON response.