2

I'm retrieving and saving images from a external source to my webserver by URL's. Unfortaley sometimes I get the following error: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error.

When I visit the given url with my browser the image is shown and valid. The url:

http://****.com/00/s/NTgwWDcyNg==/z/7LEAAMXQVT9TCcxx/$_85.JPG

The code which I use:

$opts = array('http'=>array('header' => "User-Agent:Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.75 Safari/537.1\r\n", 'timeout' => "360"));
$context = stream_context_create($opts);                                         
$imageString = file_get_contents(urldecode(urlencode($filename)), false, $context);                                            
$save = file_put_contents(dirname(dirname(__FILE__)) . '/tmp/' . $id . '_' . $sequenceNumber . '.jpg', $imageString);

Anyone has a idea why I get a 500 error?

user3009108
  • 57
  • 1
  • 1
  • 9
  • whats with urlencode and urldecode ? – guy_fawkes Feb 23 '14 at 17:40
  • i've had tested a other image url with phpfiddle which gave also a 500 error, with urlencode/urldecode i've fixed this. When processing this url in phpfiddle I got a can't resolve host, fixed with a @ before file_get_contents. But what does the @? – user3009108 Feb 23 '14 at 17:57
  • http://stackoverflow.com/questions/1032161/what-is-the-use-of-symbol-in-php – miken32 Feb 23 '14 at 18:35
  • 1
    Possible duplicate of [Why I'm getting 500 error when using file\_get\_contents(), but works in a browser?](http://stackoverflow.com/questions/10524748/why-im-getting-500-error-when-using-file-get-contents-but-works-in-a-browser) – Gerard de Visser Mar 07 '16 at 10:42

1 Answers1

5

The following answer worked for me: PHP file_get_contents 500 Internal Server error

$opts = array('http' => array('header' => "User-Agent:MyAgent/1.0\r\n"));
$context = stream_context_create($opts);
$header = file_get_contents('https://www.example.com', FALSE, $context);
Community
  • 1
  • 1
David M
  • 1,151
  • 12
  • 21