【发布时间】:2012-06-19 08:50:06
【问题描述】:
这两段代码在访问 REST API 时有何不同?
$result = file_get_contents('http://api.bitly.com/v3/shorten?login=user&apiKey=key&longUrl=url');
和
$ch = curl_init('http://api.bitly.com/v3/shorten?login=user&apiKey=key&longUrl=url');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
它们都产生相同的结果,判断为
print_r(json_decode($result))
【问题讨论】:
-
cURL的能力远远超过file_get_contents。应该够了。 -
FWIW 在速度方面差别不大。我刚刚完成了 5,000 个 URL 的获取并将它们的 HTML 保存到文件中(每个文件大约 200k)。我用 curl 做了一半,用 file_get_contents 做了一半,没有明显的区别。
-
可以使用 file_get_contents 发送 post 数据,只要您使用支持流上下文的版本。
标签: php curl file-get-contents