file_get_contents或curl在php? [英] file_get_contents or curl in php?
问题描述
在PHP中应使用 file_get_contents
或 curl
发出HTTP请求?
如果 file_get_contents
将执行此作业,是否需要使用 curl
?使用 curl
似乎需要更多行。
例如:
curl:
$ ch = curl_init 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);
有很多选项要设置。你可以真正设置任何你需要的选项。
file_get_contents()
真的只是GETs或POSTs文件并具有结果。
/ p>
我试过一些api的,这也有一些时间:
curl比file_get_contents / p>
只需使用终端尝试:
time php dev / test / curl。 php
curl.php:
<?php
$ ch = curl_init();
$ options = [
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'http://api.local/all'
];
curl_setopt_array($ ch,$ options);
$ data = json_decode(curl_exec($ ch));
curl_close($ ch);
fgc.php
<?php
$ data = json_decode(file_get_contents('http://api.local/all'));
平均卷曲比file_get_contents快3-10倍
我不认为这是巧合。
Which of file_get_contents
or curl
should be used in PHP to make an HTTP request?
If file_get_contents
will do the job, is there any need to use curl
? Using curl
seems to need more lines.
eg:
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);
First of all curl has a lot of options to set. you can really set any option you need to.
file_get_contents()
really just GETs or POSTs the file and has the result.
But!
I tried some api's and this also some times:
curl was a lot faster as file_get_contents()!
Just try it with your terminal:
time php dev/test/curl.php
curl.php:
<?php
$ch = curl_init();
$options = [
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'http://api.local/all'
];
curl_setopt_array($ch, $options);
$data = json_decode(curl_exec($ch));
curl_close($ch);
fgc.php
<?php
$data = json_decode(file_get_contents('http://api.local/all'));
Averaged curl was 3-10 times faster than file_get_contents
I don't think it was coincidence.
这篇关于file_get_contents或curl在php?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!