php fopen,file_get_contents,curl的区别
2013-08-22 15:59
691 查看
1. fopen
/file_get_contents 每次请求都会重新做DNS查询,并不对DNS信息进行缓存。但是CURL会自动对DNS信息进行缓存。对同一域名下的网页或者图片的请求只需要一次DNS查询。这大大减少了DNS查询的次数。所以CURL的性能比fopen
/file_get_contents 好很多。
2. fopen
/file_get_contents在请求HTTP时,使用的是http_fopen_wrapper,不会keeplive。而curl却可以。这样在多次请求多个链接时,curl效率会好一些。
3. fopen
/ file_get_contents函数会受到php.ini文件中allow_url_open选项配置的影响。如果该配置关闭了,则该函数也就失效了。而curl不受该配置的影响。
4. curl可以模拟多种请求,例如:POST数据,表单提交等,用户可以按照自己的需求来定制请求。而fopen
/ file_get_contents只能使用get方式获取数据。
下面的函数需要php curl模块的支持
/**
* 抓取的url链接内容
* @param string $url 要抓取的url链接,可以是http,https链接
* @param int $second 设置cURL允许执行的最长秒数
* @return mixed
*/
function get_curl_contents($url, $second = 30)
{
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_TIMEOUT,$second);//设置cURL允许执行的最长秒数
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);//当此项为true时,curl_exec($ch)返回的是内容;为false时,curl_exec($ch)返回的是true/false
//以下两项设置为FALSE时,$url可以为"https://login.yahoo.com"协议
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
同时,file_get_contents()函数获取https链接内容的时候,需要php 中,mod_ssl的支持(或安装opensll)
/file_get_contents 每次请求都会重新做DNS查询,并不对DNS信息进行缓存。但是CURL会自动对DNS信息进行缓存。对同一域名下的网页或者图片的请求只需要一次DNS查询。这大大减少了DNS查询的次数。所以CURL的性能比fopen
/file_get_contents 好很多。
2. fopen
/file_get_contents在请求HTTP时,使用的是http_fopen_wrapper,不会keeplive。而curl却可以。这样在多次请求多个链接时,curl效率会好一些。
3. fopen
/ file_get_contents函数会受到php.ini文件中allow_url_open选项配置的影响。如果该配置关闭了,则该函数也就失效了。而curl不受该配置的影响。
4. curl可以模拟多种请求,例如:POST数据,表单提交等,用户可以按照自己的需求来定制请求。而fopen
/ file_get_contents只能使用get方式获取数据。
下面的函数需要php curl模块的支持
/**
* 抓取的url链接内容
* @param string $url 要抓取的url链接,可以是http,https链接
* @param int $second 设置cURL允许执行的最长秒数
* @return mixed
*/
function get_curl_contents($url, $second = 30)
{
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_TIMEOUT,$second);//设置cURL允许执行的最长秒数
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);//当此项为true时,curl_exec($ch)返回的是内容;为false时,curl_exec($ch)返回的是true/false
//以下两项设置为FALSE时,$url可以为"https://login.yahoo.com"协议
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
同时,file_get_contents()函数获取https链接内容的时候,需要php 中,mod_ssl的支持(或安装opensll)
相关文章推荐
- PHP中fopen,file_get_contents,curl的区别
- PHP中fopen,file_get_contents,curl函数的区别
- curl、fopen和file_get_contents区别
- php fopen与file_get_contents的区别
- php读取网络文件curl,fsockopen,file_get_contents,file,fopen几种方法
- PHP中fopen,file_get_contents,curl函数的区别
- nginx+fastcgi php 使用file_get_contents、curl、fopen读取localhost本站点.php异常的情况
- PHP中fopen,file_get_contents,curl函数的区别和性能
- fopen,file_get_contents,curl的区别
- fopen,file_get_contents,curl的区别[zz]
- windows nginx使用file_get_contents、fopen、curl访问php文件造成阻塞的解决办法
- php fopen()和file_get_contents() 区别介绍
- curl、fopen和file_get_contents区别
- file_get_contents与curl的区别
- php读取本地文件常用函数(fopen与file_get_contents)
- 解决PHP无法通过file_get_contents或curl采集页面内容
- fopen,file_get_contents,curl
- php中file_get_contents与curl性能比较分析
- php读取网络文件(curl, fsockopen ,file_get_contents 几个方法的效率对比)
- fopen()和file_get_contents()打开URL获得网页内容的用法区别