I am attempting to use cURL to download an image from an external server. This code works on my local Windows XAMPP installation:
$ch = curl_init("http://some.server.com/some-image.jpg");
$fp = fopen("testimage.jpg", 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
However, when I take that exact same code and run it on my production server, the image does not get successfully downloaded. testimage.jpg gets created, but with a 0 file size. A call to curl_errno returns 0. Are there any system settings that affect how cURL operates?
For the record, the url I am using does cause a redirect to occur. When I enter the url into a browser, a different url ends up in the address bar when the image is displayed. However, I was under the impression that CURLOPT_FOLLOWLOCATION was all that was needed to follow redirects, particularly because this is all that I need to make this work locally. Am I missing something?
Firstly make sure you explicitly envoke display errors and error_reporting by adding the following:
Also check your firewall settings to ensure that the request can successfully be sent through.