php - file_get_contents 无法打开流 : HTTP request failed! HTTP/1.1 500 内部服务器错误

标签 php image file-get-contents urlencode

我正在通过 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/

相关文章:

php - 查询字符串是如何工作的

php - 从数组中获取三个最大值

javascript - Canvas - 更改网页上所有图像的颜色(例如,灰度)

php - PHP file_get_contents 有限制吗?

c - 文件 I/O 问题 - 冲突的 while 循环

php - 是否所有 MD5 十六进制哈希表示都以数字开头?

php - 如何使用 phpmyadmin 将具有相同架构的两个表合并为一个表

java - Android:BitmapFactory 返回 java.io.FileNotFoundException 错误,即使图像已保存到外部存储

css - img 宽度 100% 显示 : box (css flexbox parent)

php - 使用PHP和JSON的YouTube视频订阅用户名和电子邮件ID