开发者

cURL acting differently on different servers

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:

ini_set('display_errors', 'on');
error_reporting(E_ALL);

Also check your firewall settings to ensure that the request can successfully be sent through.


There is a setting for curl called verbose,

curl_setopt( $ch, CURLOPT_VERBOSE, true );

I have found this very helpful in situations like this. It will print out everything curl does and any HTTP codes encountered. Set this and let us know what errors your getting.

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜