【问题标题】:file_get_contents failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Errorfile_get_contents 无法打开流:HTTP 请求失败! HTTP/1.1 500 内部服务器错误
【发布时间】:2014-03-25 04:09:48
【问题描述】:

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

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

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 image file-get-contents urlencode


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-02
    • 2017-08-07
    • 2011-05-31
    • 2012-03-23
    • 1970-01-01
    • 2020-01-21
    • 2023-01-01
    相关资源
    最近更新 更多