【发布时间】:2022-01-13 11:32:13
【问题描述】:
我想用 php 检索远程托管图像。该图像存在,我可以使用我的浏览器访问它。然而 curl_exec() 的结果是空的并且 curl_error() 说:
无法连接到 img107.xooimage.com 端口 80:连接被拒绝
这是我的代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'http://img107.xooimage.com/files/5/0/b/ss-2014-06-15-at-12.58.47--46324f5.png');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$image = curl_exec($ch);
if( empty($image) ){
echo("Impossible to retrieve the file !<br>
error: ".curl_error($ch)."<br>");
}
如果我可以用我的浏览器打开图片,那为什么我使用 curl 时连接被拒绝?
备注:它适用于我自己域中的图像,但不适用于示例中的外部图像。
编辑:这实际上似乎不是 php 问题,因为我什至无法从我的网站的主机服务器执行 curl 或 ping:
curl http://img107.xooimage.com/files/5/0/b/ss-2014-06-15-at-12.58.47--46324f5.png > image.png
Connection Refused
ping http://img107.xooimage.com
ping: unknown host http://img107.xooimage.com
ping http://www.google.com
ping: unknown host http://www.google.com
也许我的托管服务提供商应用了一些限制/防火墙。
【问题讨论】:
-
这个特定的服务器可能正在过滤脚本,使用某种用户代理白名单。尝试传入浏览器的用户代理,作为第一次尝试。可能还有其他过滤服务器端的方法。使用
file_get_contents()可以工作吗?还允许 url fopen 必须启用到 php ini -
file_get_contents() 也返回空。我已经使用 phpInfo() 检查了允许 url fopen 是否打开。我会尝试用户代理技巧。
-
使用用户代理技巧
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)');,我仍然遇到同样的错误
标签: php image curl connection-refused