PHP ini file_get_contents 外部 url
我使用以下 PHP 函数:
file_get_contents('http://example.com');
每当我在某个服务器上执行此操作时,结果都是空的。当我在其他地方执行此操作时,结果就是页面的内容是什么。然而,当我在结果为空的服务器上本地使用该函数时 - 无需访问外部 URL (file_get_contents('../simple/internal/path.html');
),它确实有效。
现在,我很确定它与某个 php.ini 配置有关。但我不确定的是,是哪一个。请帮忙。
I use following PHP function:
file_get_contents('http://example.com');
Whenever I do this on a certain server, the result is empty. When I do it anywhere else, the result is whatever the page's content may be. When I however, on the server where the result is empty, use the function locally - without accessing an external URL (file_get_contents('../simple/internal/path.html');
), it does work.
Now, I am pretty sure it has something to do with a certain php.ini configuration. What I am however not sure about is, which one. Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
补充 Aillyn 的答案,您可以使用如下所示的函数来模仿 file_get_contents 的行为:
Complementing Aillyn's answer, you could use a function like the one below to mimic the behavior of file_get_contents:
您要查找的设置是
allow_url_fopen
。您有两种方法可以在不更改 php.ini 的情况下解决这个问题,其中之一是使用 < code>fsockopen(),另一种是使用 cURL 。
无论如何,我建议使用 cURL 而不是 file_get_contents(),因为它是为此构建的。
The setting you are looking for is
allow_url_fopen
.You have two ways of getting around it without changing php.ini, one of them is to use
fsockopen()
, and the other is to use cURL.I recommend using cURL over
file_get_contents()
anyways, since it was built for this.最适合 http url,
但如何打开 https url 帮助我
is best for http url,
But how to open https url help me
与ini配置设置相关
allow_url_fopen
。您应该意识到,启用该选项可能会使代码中的一些错误可被利用。
例如,验证输入失败可能会变成一个成熟的远程代码执行漏洞:
The is related to the ini configuration setting
allow_url_fopen
.You should be aware that enable that option may make some bugs in your code exploitable.
For instance, this failure to validate input may turn into a full-fledged remote code execution vulnerability:
上面提供的答案解决了问题,但没有解释OP描述的奇怪行为。这个解释应该可以帮助任何人在开发环境中测试站点之间的通信,其中这些站点都驻留在同一主机上(和同一虚拟主机;我正在使用 apache 2.4 和 php7.0)。
我发现 file_get_contents() 有一个微妙之处,它在这里绝对相关,但没有得到解决(可能是因为它要么几乎没有记录,要么据我所知没有记录,或者记录在一个晦涩的 php 安全模型白皮书中)找不到)。
在所有相关上下文中将
allow_url_fopen
设置为Off
(例如/etc/php/7.0/apache2/php.ini
、/etc /php/7.0/fpm/php.ini
等...)和allow_url_fopen
在命令行上下文中设置为On
(即/ etc/php/7.0/cli/php.ini
),将允许调用本地资源的file_get_contents()
并且不会记录任何警告,例如:或
或
最后,限制
allow_url_fopen = Off
类似于OUTPUT
链中的iptables
规则,其中仅当尝试“退出系统”或“更改上下文”被创建。注意:我在命令行上下文中将
allow_url_fopen
设置为On
(即/etc/php/7.0/cli/php.ini
)在我的系统上,但我怀疑即使将其设置为“关闭”,它也不会与我提供的解释产生任何影响,除非您通过从命令行本身运行脚本来进行测试。我没有在命令行上下文中将allow_url_fopen
设置为Off
来测试行为。The answers provided above solve the problem but don't explain the strange behaviour the OP described. This explanation should help anyone testing communication between sites in a development environment where these sites all reside on the same host (and the same virtualhost; I'm working with apache 2.4 and php7.0).
There's a subtlety with
file_get_contents()
I came across that is absolutely relevant here but unaddressed (probably because it's either barely documented or not documented from what I can tell or is documented in an obscure php security model whitepaper I can't find).With
allow_url_fopen
set toOff
in all relevant contexts (e.g./etc/php/7.0/apache2/php.ini
,/etc/php/7.0/fpm/php.ini
, etc...) andallow_url_fopen
set toOn
in the command line context (i.e./etc/php/7.0/cli/php.ini
), calls tofile_get_contents()
for a local resource will be allowed and no warning will be logged such as:or
or
To conclude, the restriction
allow_url_fopen = Off
is analogous to aniptables
rule in theOUTPUT
chain, where the restriction is only applied when an attempt to "exit the system" or "change contexts" is made.N.B.
allow_url_fopen
set toOn
in the command line context (i.e./etc/php/7.0/cli/php.ini
) is what I had on my system but I suspect it would have no bearing on the explanation I provided even if it were set toOff
unless of course you're testing by running your scripts from the command line itself. I did not test the behaviour withallow_url_fopen
set toOff
in the command line context.这也将为外部链接提供绝对路径,而无需使用 php.ini
This will also give external links an absolute path without having to use php.ini
从 cPanel 或 PHP INI 部分中的 WHM 启用allow_url_fopen
Enable allow_url_fopen From cPanel Or WHM in PHP INI Section
添加:
在您的
php.ini
文件中。如果您使用共享主机,请先创建一个。Add:
in your
php.ini
file. If you are using shared hosting, create one first.