Get youtube title from a video game in PHP using API v3

Here is the URL to get information about the video, where you must put the VIDEO-ID and API KEY:

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

      

How do I get the title from it and store it as a variable in PHP?

+3


source to share


2 answers


Something along these lines will provide you with information specific 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>

      



Another 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>

      

+4


source


There are two PHP examples in google developers .



Alternatively, you can try the test at the bottom of this page. Enter the fields part

(snippet) and id

(select one youtube id). It will show you a GET request and a JSON response.

+2


source







All Articles