PHP cURL与file_get_contents

访问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))


蝴蝶刀刀
浏览 403回答 3
3回答

慕尼黑5688855

file_get_contents()是一个简单的螺丝刀。非常适合简单的GET请求,其中标头,HTTP请求方法,超时,cookiejar,重定向和其他重要内容无关紧要。fopen()带有流上下文或带有setopt的 cURL 的Powerdrill带有您可以想到的每一个细节。

BIG阳

除此之外,由于最近的一些网站黑客攻击,我们不得不进一步保护我们的网站。这样做时,我们发现file_get_contents无法正常工作,而curl仍然可以正常工作。不是100%,但我认为此php.ini设置可能已阻止了file_get_contents请求。; Disable allow_url_fopen for security reasonsallow_url_fopen = 0无论哪种方式,我们的代码现在都可以使用curl。
打开App,查看更多内容
随时随地看视频慕课网APP