Question

I have uploaded like 100 videos to my channel,I need to get all the video ID , But everywhere i search there is code for only listing videos inside playlist.but all my videos are in root.

$channelsResponse = $youTubeService->channels->listChannels('id,contentDetails', array(
    'mine' => 'true'));
$searchResponse = $youTubeService->playlistItems->listPlaylistItems('snippet', array(
        'maxResults' => 50,
        'fields' => 'items(snippet(publishedAt,channelId,title,description,thumbnails(default),resourceId)),pageInfo,nextPageToken'));
echo json_encode($searchResponse['items']['contentDetails']['videoId']);

Above code gets videos from playlist.how can i get videos from root.

Was it helpful?

Solution

There is nothing like root, everything is in playlists in YouTube. I believe what you are asking is to get your uploaded videos. There is also a playlist called "uploads" in your channel. That's where your channels uploaded videos added.

You can get the playlistID of that playlist in your listChannels call. In the response, look for contentDetails.relatedPlaylists.uploads.

Then in your second call (listPlaylistItems), use that id to set playlistId

Here's the PHP sample.

OTHER TIPS

working solution/exmample:

function getSslPage($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

//params
$order = "date";
$part = "snippet";
$channel_id = "UCzocxfp9lrNal14glrFSrng";
$max_results = 50;
$api_key = "YOUR_API_KEY";

$feedurl = 'https://www.googleapis.com/youtube/v3/search?order='.$order.'&part='.$part.'&channelId='.$channel_id.'&maxResults='.$max_results.'&key='.$api_key;

$feed = getSslPage($feedurl);
$feed_arr = json_decode($feed, true);

//print_r($feed_arr);

$items = $feed_arr['items'];

foreach($items as $item) {
   if( (isset($item['id']['videoId'])) OR (!empty($item['id']['videoId'])) ) {
        echo '<iframe width="420" height="315"
src="http://www.youtube.com/embed/'.$item['id']['videoId'].'?autoplay=0">
</iframe>';
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top