+2 votes
in Programming Languages by (56.5k points)

I am trying to fetch images file from YouTube using file_get_contents(), but it takes 10-20 seconds to fetch one image file of size 15-25kb.

$cont=file_get_contents($img_url);

How can I speed up the download of images?

1 Answer

+1 vote
by (348k points)
selected by
 
Best answer

There may be some issues with your server settings, otherwise, file_get_contents() should not be that slow. I would recommend using curl instead of file_get_contents(). Your download speed will improve with curl.

Try the following PHP code that uses curl to fetch the content from a URL.

$ci = curl_init();
curl_setopt($ci, CURLOPT_URL, $img_url);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
$cont = curl_exec($ci);
curl_close($ci);


...