Pregunta

I am using YouTube's get_video_info to retrieve info on a video based on the ID. This is quite slow however -- pages on my site who have videos load in like 5 seconds, whereas other pages load in 1 second on average.

The code I use looks like:

$content = file_get_contents("http://www.youtube.com/get_video_info?video_id=".$video_id);
parse_str($content, $ytarr);   

Then, as needed, I can use for example $ytarr['title'] $ytarr['length_seconds'] etc.

Any idea on how I could make this faster, or any great alternatives?

Thank you

¿Fue útil?

Solución

A couple of things that may be helpful.

1) It's very likely that the slowdown is caused not by the service itself, but some other factors -- for me, for example, the first page you link to (the one with the API) loads twice as fast as the one without any video. You could get a speed profiler (something like Google's pagespeed service or Yahoo's YSlow) to give you actual numbers for each of the requests, so you can tell where the problem might lie.

2) The get_video_info isn't a documented part of the YouTube API, so there isn't any official support for it ... kind of a crapshoot to rely on it, more or less! If the info that you're gleaning from it is available through the official API channels, it would likely be worth your time to make a switch. For example, you mention video views ... you could call the URL: https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id=lIC_lpo-s5I&key={YOUR_API_KEY} to get a json packet that has all the video statistics (as well as some other video info).

Good luck!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top