这两段代码在访问 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))
最佳答案
file_get_contents()
是一个简单的 Screwdriver 。非常适合 header 、HTTP 请求方法、超时、cookiejar、重定向和其他重要事项无关紧要的简单 GET 请求。
fopen()
带有 stream context或带有 setopt 的 cURL是你能想到的每一个细节和选项的电钻。
关于PHP cURL 与 file_get_contents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11064980/