如何使用YouTube API V3获取视频上传日期?

时间:2015-10-19 16:54:49

标签: api youtube

我知道如何获取视频时长和视图,例如

$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=hqepb5hzuB0&key={YOUR-API-KEY}");
$json_data = json_decode($JSON, true);
echo $json_data['items'][0]['statistics']['viewCount'];

但我怎样才能获得视频上传日期?

1 个答案:

答案 0 :(得分:1)

$vidkey = "Gsc7_E5HewM" ; //for example 
$apikey = "xxxxxxxxxxxxxxxxx" ;
$dur = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=$vidkey&key=$apikey");


//----- duration---
$VidDuration =json_decode($dur, true);
foreach ($VidDuration['items'] as $vidTime)     
{           
$VidDuration= $vidTime['contentDetails']['duration']; 
}


// Check if $VidDuration is ISO string so the video is ready
if (is_string($VidDuration)){   
    //convert duration from ISO to M:S
    $date = new DateTime('2000-01-01');     
    $date->add(new DateInterval($VidDuration));
    $vid_durH= $date->format('H') ; 
    if ($vid_durH=="00") {
        $vid_dur= $date->format('i:s') ;
    }
    else { 
        $vid_dur= $date->format('H:i:s') ;  
    } 
} 
else { 
    $vid_dur ="error" ;     
} 


$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=$vidkey&key=$apikey");      
$json_data = json_decode($JSON, true);      
$uploadDate = $json_data['items'][0]['snippet']['publishedAt'];         
$uploadDate = strtotime($uploadDate);
$uploadDate= date("F j, Y", $uploadDate); 

echo $vid_dur ;