contentDetails or duration not coming using Youtube v3 api

前端 未结 3 997
栀梦
栀梦 2021-01-01 01:23

Look at this link , there is an example given

https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=YOUR_API_KEY
 &part=snippet,contentDeta         


        
3条回答
  •  没有蜡笔的小新
    2021-01-01 01:30

    Using your links above, here is a quick php example on how to make only those two calls for a maximum of 50 results per call

    $JSON = file_get_contents('https://www.googleapis.com/youtube/v3/search?part=snippet&q=cats&fields=items%2CnextPageToken%2CprevPageToken%2CtokenPagination&maxResults=50&key={YOUR_API_KEY});
    

    The above link will search for cats (q=cats) and will fetch maxResults=50.

    Onwards, we store each id in a string divided by commas

    $get_duration="";
    
    foreach ($JSON_Data->items as $ids) {
    
            $get_duration .=$ids->id->videoId.",";
    }
    
    $get_duration = rtrim($get_duration, ",");
    

    Finally, we make the second call using the batch ids contained in $get_duration and display the title and duration of each video

    $JSON= file_get_contents('https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics%2Cstatus&id='.$get_duration.'&key={YOUR_API_KEY}');
    
    $JSON_Data = json_decode($JSON);
    
    foreach ($JSON_Data->items as $ids) {
    
            $date = new DateTime('1970-01-01');
            $date->add(new DateInterval($ids->contentDetails->duration));
            echo "Title: ".$ids->snippet->title."\nDuration: {$date->format('H:i:s')}\n\n";
    }
    

    The result will be something like this

    > Title: Cats Being Jerks Video Compilation || FailArmy 
    > Duration: 00:08:33
    > 
    > Title: Ultimate cat vines compilation - Best cat vines 2014 / 2015
    > Duration: 00:14:58
    > 
    > Title: Funny cats annoying owners - Cute cat compilation 
    > Duration: 00:05:58
    > 
    > Title: Funny Cats Compilation 60 min - NEW in HD 2014 
    > Duration: 00:57:51
    

提交回复
热议问题