我正在通过 URL 从外部源检索图像并将其保存到我的网络服务器。不幸的是,有时我会收到以下错误:无法打开流:HTTP 请求失败! HTTP/1.1 500 内部服务器错误。
当我使用浏览器访问给定的网址时,图像会显示并且有效。网址:
http://****.com/00/s/NTgwWDcyNg==/z/7LEAAMXQVT9TCcxx/$_85.JPG
我使用的代码:
$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);
有人知道为什么我会收到 500 错误吗?
最佳答案
以下答案对我有用: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);
关于php - file_get_contents 无法打开流 : HTTP request failed! HTTP/1.1 500 内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21971800/