问题
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?
回答1:
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);
来源:https://stackoverflow.com/questions/21971800/file-get-contents-failed-to-open-stream-http-request-failed-http-1-1-500-inter