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

use*_*108 2 php image urlencode file-get-contents

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

当我使用浏览器访问给定的URL时,图像显示并且有效.网址:

http://****.com/00/s/NTgwWDcyNg==/z/7LEAAMXQVT9TCcxx/$_85.JPG
Run Code Online (Sandbox Code Playgroud)

我使用的代码:

$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);
Run Code Online (Sandbox Code Playgroud)

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

Dav*_*d M 5

以下答案对我有用:PHP file_get_contents 500内部服务器错误

$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);
Run Code Online (Sandbox Code Playgroud)