PHP cURL 与 file_get_contents

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11064980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:41:24  来源:igfitidea点击:

PHP cURL vs file_get_contents

phpcurlfile-get-contents

提问by Salvador Dali

How do these two pieces of code differ when accessing a REST API?

访问 REST API 时,这两段代码有何不同?

$result = file_get_contents('http://api.bitly.com/v3/shorten?login=user&apiKey=key&longUrl=url');

and

$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);

They both produce the same result, judging by

他们都产生相同的结果,判断

print_r(json_decode($result))

回答by Xeoncross

file_get_contents()is a simple screwdriver. Great for simple GET requests where the header, HTTP request method, timeout, cookiejar, redirects, and other important things do not matter.

file_get_contents()是一把简单的螺丝刀。非常适合简单的 GET 请求,其中标头、HTTP 请求方法、超时、cookiejar、重定向和其他重要的事情都无关紧要。

fopen()with a stream contextor cURL with setoptare powerdrills with every bit and option you can think of.

fopen()带有流上下文或带有setopt 的cURL是具有您能想到的所有位和选项的强力练习。

回答by vr_driver

In addition to this, due to some recent website hacks we had to secure our sites more. In doing so, we discovered that file_get_contentsfailed to work, where curlstill would work.

除此之外,由于最近的一些网站黑客攻击,我们不得不更多地保护我们的网站。在这样做时,我们发现file_get_contents无法工作,而curl仍然可以工作。

Not 100%, but I believe that this php.ini setting may have been blocking the file_get_contentsrequest.

不是 100%,但我相信这个 php.ini 设置可能阻止了file_get_contents请求。

; Disable allow_url_fopen for security reasons
allow_url_fopen = 0

Either way, our code now works with curl.

无论哪种方式,我们的代码现在都可以使用curl

回答by Ivijan Stefan Stipi?

This is old topic but on my last test on one my API, cURL is faster and more stable. Sometimes file_get_contents on larger request need over 5 seconds when cURL need only from 1.4 to 1.9 seconds what is double faster.

这是一个老话题,但在我对我的 API 的最后一次测试中,cURL 更快更稳定。有时,较大请求上的 file_get_contents 需要超过 5 秒,而 cURL 只需要 1.4 到 1.9 秒,这是两倍快。

I need to add one note on this that I just send GET and recive JSON content. If you setup cURL properly, you will have a great response. Just "tell" to cURL what you need to send and what you need to recive and that's it.

我需要为此添加一个注释,我只发送 GET 并接收 JSON 内容。如果您正确设置了 cURL,您将获得很好的响应。只需“告诉”cURL 您需要发送什么以及您需要接收什么,就是这样。

On your exampe I would like to do this setup:

在你的例子中,我想做这个设置:

$ch =  curl_init('http://api.bitly.com/v3/shorten?login=user&apiKey=key&longUrl=url');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_TIMEOUT, 3);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
$result = curl_exec($ch);

This request will return data in 0.01 second max

此请求将在 0.01 秒内最多返回数据