0

I'm using this function to get YouTube video length in seconds

function getDuration($url){
parse_str(parse_url($url,PHP_URL_QUERY),$arr);
$video_id=$arr['v'];
$data=@file_get_contents('http://gdata.youtube.com/feeds/api/videos/'.$video_id.'?v=2&alt=jsonc');
if (false===$data) return false;
$obj=json_decode($data);
return $obj->data->duration;
}

But all videos return duration 236 like this video which its duration is 42 minutes and 45 seconds here's the sample

http://gdata.youtube.com/feeds/api/videos/gXGn06tuvZM?v=2&alt=jsonc

is this something wrong with the function or with the youtube api as I noticed when I open youtube website all videos' duration has gone as in the image

enter image description here

PHP User
  • 2,350
  • 6
  • 46
  • 87

2 Answers2

1

The GData API has been retired, and will only return a "device not supported" video no matter what request you send (Which, I guess, is 236 seconds). You'll need to switch to V3 of the API.

jlmcdonald
  • 13,408
  • 2
  • 54
  • 64
0

followed the steps hete to create a new app and get a new API key google documentation then used this answer stackoverflow question then it was simple to split the duration into an array and retrieve values

https://www.googleapis.com/youtube/v3/videos?id=9bZkp7q19f0&part=contentDetails
&key={YOUR_API_KEY}
Community
  • 1
  • 1
PHP User
  • 2,350
  • 6
  • 46
  • 87