php file_get_contents 无法打开流:HTTP 请求失败!HTTP/1.1 500 内部服务器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21971800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:35:20  来源:igfitidea点击:

file_get_contents failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error

phpimagefile-get-contentsurlencode

提问by user3009108

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.

我正在通过 URL 从外部源检索图像并将其保存到我的网络服务器。不幸的是,有时我会收到以下错误:无法打开流:HTTP 请求失败!HTTP/1.1 500 内部服务器错误。

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

当我使用浏览器访问给定的 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?

任何人都知道为什么我会收到 500 错误?

回答by David M

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

以下答案对我有用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);