【发布时间】:2012-10-11 20:55:13
【问题描述】:
在 PHP 中应该使用 file_get_contents 或 curl 中的哪一个来发出 HTTP 请求?
如果file_get_contents 可以完成这项工作,是否需要使用curl?使用curl 似乎需要更多的行。
例如:
卷曲:
$ch = curl_init('http://www.website.com/myfile.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
curl_close ($ch);
file_get_contents:
$output = file_get_contents('http://www.website.com/myfile.php'.$content);
【问题讨论】:
-
curl 可以比 file_get_contents() 做更多的事情,但是如果您不需要它做的任何事情,那么请采用更简单的方法。
-
我听说,使用 file_get_contents 有一些安全威胁,所以很少有服务器在 PHP 中禁用此功能。
-
@Dagon phpsec.org/projects/phpsecinfo/tests/allow_url_fopen.html 这是我第一次读到关于安全问题的地方。此外,cURL 似乎比 file_get_contents 快。这是一个关于相同的好帖子 -> stackoverflow.com/questions/555523/…
-
@Dagon 在我以前的工作中,我们的企业 PHP 包禁用了 allow_url_fopen,因此在抓取 Web 服务时我们不得不使用 cURL。不确定具体问题是什么,但使用 cURL,您可以执行诸如在帖子中传递登录信息之类的操作,并且比使用 file_get_contents 更灵活地处理返回的数据。
-
@teami 特定于 include() 和 require(),而不是 op 的 file_get_contents 问题
标签: php curl file-get-contents