Sunday, May 15, 2016

Retrieve video ids from playlist id - youtube api v3

http://stackoverflow.com/questions/26817919/retrieve-video-ids-from-playlist-id-youtube-api-v3

http://www.codeproject.com/Tips/998364/Youtube-video-list-with-PHP


I am currently working with youtube api v3. I am trying to retrieve video ids from a particular playlist. I have search through the documentation but can’t find an example that references that or something similar. I am using this repo from GITHUB. How can I retrieve video ids from a given playlist id?
Here is a snippet but it only yields general information of the playlist:
require 'vendor/autoload.php';

use Madcoda\Youtube;

$youtube = new Youtube(array('key' => '<API KEY>'));

// Return a std PHP object
$playlist = $youtube->getPlaylistById('PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs');

echo "<pre>";
print_r($playlist);
echo "</pre>"
shareimprove this question


Yes, you can !
With the YouTube API v3 with the ressource playlistItems.list
Use this parameters to get the video ID of a playlist :
part: 'contentDetails'
playlistId: 'PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs'
The output :
 "items": [
  {

   "kind": "youtube#playlistItem",
   "etag": "\"PSjn-HSKiX6orvNhGZvglLI2lvk/OCpNe6AE7ww8_WCYOw2M6axwxAY\"",
   "id": "PLDWb5NF1kx1qB7HgQ2tJKvLCsn0D34rFYWn3o4SsrJrU",
   "contentDetails": {
    "videoId": "Lv-sY_z8MNs"
   }
  },
  {

   "kind": "youtube#playlistItem",
   "etag": "\"PSjn-HSKiX6orvNhGZvglLI2lvk/Qvv0GzRVKvhc4LANFBV171hvlJI\"",
   "id": "PLDWb5NF1kx1qB7HgQ2tJKvAZrOtpGhFVaTYQJQCi66rE",
   "contentDetails": {
    "videoId": "xY_MUB8adEQ"
   }
  },
  {

   "kind": "youtube#playlistItem",
   "etag": "\"PSjn-HSKiX6orvNhGZvglLI2lvk/rRRgwge6ptZxyMRs-RzsvEFU6qs\"",
   "id": "PLDWb5NF1kx1qB7HgQ2tJKvAI1eXgw8QWwXeA8Anwu4VY",
   "contentDetails": {
    "videoId": "xcc9S_ik0v8"
   }
  }
...
Then you have the video IDs of the playlist.
I look up quickly the github you link into in your post, you would have something like this : ( /!\ I haven't tested this code/!)
use Madcoda\Youtube;

$youtube = new Youtube(array('key' => '/* Your API key here */'));

// Set Default Parameters
$params = array(
    'part'          => 'contentDetails',
);

$playlistItems = $youtube->getPlaylistItemsByPlaylistId('PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs');

print_r($playlistItems); 
shareimprove this answer
  
Great, i did now just test it but i get undefined index: results – Code_Ed_Student Nov 9 '14 at 0:09
  
@Code_Ed_Student if you don't have the base for coding i can't help you. – mpgn Nov 9 '14 at 9:51
  
Figured it out. Thanks! – Code_Ed_Student Nov 9 '14 at 12:43
  
I've found that this does not seem to work with the PHP library. A response is sent, but items array is blank. – Solid I Mar 11 '15 at 23:55