我是否使用 fopen 或 curl 来加载 PHP 中给定 URL 的 XML 文件

发布于 2024-08-30 23:41:26 字数 151 浏览 3 评论 0 原文

我有一个可以通过 URL 获取的 XML 文件。我知道我可以使用 fopen 获取文件,但有时我看到脚本使用 curl。使用 curl 比使用 fopen 获取 XML 文件有优势吗?

I have an XML file I can get via a URL. I know I can get the file using fopen, but sometimes I've seen scripts use curl. Is there an advantage to using curl over fopen to get XML files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

调妓 2024-09-06 23:41:26

allow_url_fopen,如果你想用fopen打开远程文件是必需的,可以禁用;因此,在某些情况下 fopen('http://...') 是不可能的。

注意:在这个答案中,我说“fopen”,但它与所有可以访问远程文件的 PHP 函数完全相同:fopenfile_get_contents , simplexml_load_file, ...

另一方面,curl 是一个扩展,并不总是启用。

One nice with curl is that it's pretty easy to configure, and there are a lot of existing options *(see [`curl_setopt`][3])*

要配置 fopen 访问远程文件的方式,有点棘手——您通常必须使用流(请参阅此处,例如) ;而且,一般来说,了解curl的人比掌握stream的开发人员还要多。

Safest way -- especially if your application will be deployed to servers on which you are not administrator, and cannot re-configure :

  • 尝试一种解决方案
  • ,如果不起作用,请尝试另一种解决方案

allow_url_fopen, which is required if you want to open a remote file with fopen, can be disabled ; so, there are situations in which fopen('http://...') is not possible.

Note : in this answer, I say "fopen", but it's exactly the same with all PHP functions that can access remote files : fopen, file_get_contents, simplexml_load_file, ...

On the other hand, curl is an extension, and is not always enabled either.

One nice with curl is that it's pretty easy to configure, and there are a lot of existing options *(see [`curl_setopt`][3])*

To configure the way fopen accesses remote files, it's a bit trickier -- you'll generally have to work with streams (see here, for example) ; and, generally speaking, there are more people knowing curl than there are developpers mastering streams.

Safest way -- especially if your application will be deployed to servers on which you are not administrator, and cannot re-configure :

  • Try one solution
  • And, if it doesn't work, try the other one
狼亦尘 2024-09-06 23:41:26

好吧,如果您要使用 SimpleXML 加载文件,您可以使用

simplexml_load_file($filename);

但是,某些服务器会限制从此功能加载 url。在这种情况下,您将被限制使用 cURL。

Well, if you are going to use SimpleXML to load the file you can use

simplexml_load_file($filename);

However, some servers will restrict loading urls from this function. In this case you would be restricted to cURL.

雪落纷纷 2024-09-06 23:41:26

fopen 使用起来更简单,而且我认为并非所有服务器设置都支持开箱即用的 curl。如果 fopen 适合您,那么它可能是您的最佳选择。

fopen is simpler to use, and I think not all server setups support curl out of the box. If fopen works fine for you it's probably your best choice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文