Reputation: 1190
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>"
Upvotes: 1
Views: 3124
Reputation: 7251
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'
https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&playlistId=PL590L5WQmH8fJ54F369BLDSqIwcs-TCfs&key={YOUR_API_KEY}
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);
Upvotes: 3