使用fopen()或file_get_contents()检索网页的最佳实践

问题描述 投票:4回答:2

我正在寻找一些关于从网页(我自己的网站)检索信息的最佳方法的建议,然后搜索该信息以查找某个文本。

请记住,某些运行PHP / Joomla的服务器没有启用cURL,我不想导致任何意外错误。

我看过fopen()file_get_contents(),两者似乎都有相互矛盾的看法,无论是否有cURL都可以使用。

php joomla joomla1.5
2个回答
4
投票

它们都可以在没有卷曲的情况下工作,但你需要启用allow_url_fopen。您可以通过执行phpinfo()来检查。但是有安全隐患,请看:

Should I allow 'allow_url_fopen' in PHP?

因此,要获取页面,您可以使用fopen(),file_get_contents()或其他一些在PHP中实现的独立HTTP客户端,例如HttpClient,它们可以在没有这些功能的情况下运行。


1
投票
I read now and then on forums and chats that allow_url_fopen has to be enabled for Joomla to work properly. That's an "urban legend", leftover from old versions, but almost no one seems to actually test and confirm that fact. It seems the Docs are also wrong about it.

Joomla! CMS & Framework don't depend on "allow_url_fopen" being enabled. Period!

Quite the contrary, J! Framework has a very good and robust core subpackage HTTP, which supports "drivers" based on cURL, streams and sockets. Which means even with fopen() URL wrappers ("streams" driver) disabled altogether ("allow_url_fopen = 0"), any extensions implementing HTTP subpackage still has "cURL" and "sockets" driver available. For example, Joomla Update component will, in case "allow_url_fopen" is disabled, fall back on using cURL wrapper (given cURL PHP extension is enabled).
© www.soinside.com 2019 - 2024. All rights reserved.