`

curl, fsockopen ,file_get_contents 几个方法的效率对比

 
阅读更多

php读取网络文件 curl, fsockopen ,file_get_contents 几个方法的效率对比

curl效率及稳定原来可以远远超越file_get_contents

 

最近需要获取别人网站上的音乐数据。用了file_get_contents函数,但是总是会遇到获取失败的问题,尽管按照手册中的 例子设置了超时,可多数时候不会奏效:


$config['context'] = stream_context_create(array(‘http’ => array(‘method’ => “GET”, 
   ’timeout’ => 5//这个超时时间不稳定,经常不奏效 
   ) 
  )); 

这时候,看一下服务器的连接池,会发现一堆类似的错误,让我头疼万分: 
file_get_contents(http://***): failed to open stream… 

现在改用了curl库,写了一个函数替换: 
function curl_file_get_contents($durl){ 
  $ch = curl_init(); 
  curl_setopt($ch, CURLOPT_URL, $durl); 
  curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
  curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_); 
  curl_setopt($ch, CURLOPT_REFERER,_REFERER_); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  $r = curl_exec($ch); 
  curl_close($ch); 
   return $r; 
} 

如此,除了真正的网络问题外,没再出现任何问题。 
这是别人做过的关于curl和file_get_contents的测试: 
file_get_contents抓取google.com需用秒数: 

 

2.31319094

2.30374217 
2.21512604 
3.30553889 
2.30124092 

curl使用的时间: 

 

0.68719101

0.64675593 
0.64326 
0.81983113 
0.63956594 

差距很大?呵呵,从我使用的经验来说,这两个工具不只是速度有差异,稳定性也相差很大。 

建议对网络数据抓取稳定性要求比较高的朋友使用上面的 curl_file_get_contents函数,不但稳定速度快,还能假冒浏览器欺骗目标地址哦!

 

 

 

看到的其他文章收藏于此===============================

php fsockopen

方法1: 用file_get_contents 以get方式获取内容
<?php
$url='http://www.domain.com/';
$html = file_get_contents($url);
echo $html;
?>

方法2: 用fopen打开url, 以get方式获取内容
<?php
$fp = fopen($url, 'r');
stream_get_meta_data($fp);
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
echo "url body: $result";
fclose($fp);
?>

 

 

方法3:用file_get_contents函数,以post方式获取url
<?php
$data = array ('foo' => 'bar');
$data = http_build_query($data);

$opts = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);

$context = stream_context_create($opts);
$html = file_get_contents('http://localhost/e/admin/test.html', false, $context);

echo $html;
?>

 

方法4:用fsockopen函数打开url,以get方式获取完整的数据,包括header和body

<?php
function get_url ($url,$cookie=false)
{
$url = parse_url($url);
$query = $url[path]."?".$url[query];
echo "Query:".$query;
$fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);
if (!$fp) {
return false;
} else {
$request = "GET $query HTTP/1.1\r\n";
$request .= "Host: $url[host]\r\n";
$request .= "Connection: Close\r\n";
if($cookie) $request.="Cookie:   $cookie\n";
$request.="\r\n";
fwrite($fp,$request);
while()) {
$result .= @fgets($fp, 1024);
}
fclose($fp);
return $result;
}
}
//获取url的html部分,去掉header
function GetUrlHTML($url,$cookie=false)
{
$rowdata = get_url($url,$cookie);
if($rowdata)
{
$body= stristr($rowdata,"\r\n\r\n");
$body=substr($body,4,strlen($body));
return $body;
}

    return false;
}
?>

 

 

方法5:用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body

<?php
function HTTP_Post($URL,$data,$cookie, $referrer="")
{

    // parsing the given URL
$URL_Info=parse_url($URL);

    // Building referrer
if($referrer=="") // if not given use this script as referrer
$referrer="111";

    // making string from $data
foreach($data as $key=>$value)
$values[]="$key=".urlencode($value);
$data_string=implode("&",$values);

    // Find out which port is needed - if not given use standard (=80)
if(!isset($URL_Info["port"]))
$URL_Info["port"]=80;

    // building POST-request:
$request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
$request.="Host: ".$URL_Info["host"]."\n";
$request.="Referer: $referer\n";
$request.="Content-type: application/x-www-form-urlencoded\n";
$request.="Content-length: ".strlen($data_string)."\n";
$request.="Connection: close\n";

    $request.="Cookie:   $cookie\n";

    $request.="\n";
$request.=$data_string."\n";

    $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
fputs($fp, $request);
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
fclose($fp);

    return $result;
}

?>

 

方法6:使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展

<?php
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, 'http://www.domain.com/');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);

echo $file_contents;
?>

 

分享到:
评论

相关推荐

    比file_get_contents稳定的curl_get_contents分享

    分享一个实际在用的函数: 复制代码 代码如下: /*比file_get_contents稳定的多!$timeout为超时时间,单位是秒,默认为1s。*/ function curl_get_contents($url,$timeout=1) { $curlHandle = curl_init(); curl_...

    php基于curl重写file_get_contents函数实例

    另外,curl的性能比file_get_contents高,所以用curl重写file_get_contents function _file_get_contents&#40;$s&#41; { $ret = ; $ch = curl_init($s); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch...

    解决PHP curl或file_get_contents下载图片损坏或无法打开的问题

    今天小编就为大家分享一篇解决PHP curl或file_get_contents下载图片损坏或无法打开的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

    关于file_get_contents返回为空或函数不可用的解决方案

    如果你使用file_get_contents获取远程文件内容返回为空或提示该函数不可用,也许本文能帮到你! 使用file_get_contents和fopen必须空间开启allow_url_fopen。方法:编辑php.ini,设置allow_url_fopen = On,allow_url...

    PHP curl 或 file_get_contents 获取需要授权页面的方法

    今天因工作需要,需要用 curl / file_get_contents 获取需要授权(Authorization)的页面内容,解决后写了这篇文章分享给大家。 PHP curl 扩展,能够在服务器端发起POST/GET请求,访问页面,并能获取页面的返回数据。 ...

    深入file_get_contents与curl函数的详解

    下面是file_get_contents和curl两个函数同样功能的不同写法file_get_contents函数的使用示例:复制代码 代码如下:&lt; ?php$file_contents = file_get_contents&#40;‘https://www.jb51.net’&#41;;echo $file_...

    深入file_get_contents函数抓取内容失败的原因分析

    下面是file_get_contents和curl两个函数同样功能的不同写法file_get_contents函数的使用示例:复制代码 代码如下:&lt; ?php$file_contents = file_get_contents&#40;‘https://www.jb51.net’&#41;;echo $file_...

    解决file_get_contents无法请求https连接的方法

    错误: Warning: fopen&#40;&#41; [function.fopen]: Unable to find the wrapper “https” – did you forget to enable it ...3.如果服务器你不能修改配置的话,那么就使用curl函数来替代file_get_contents函数,

    php中使用Curl、socket、file_get_contents三种方法POST提交数据

    抓取远程内容,之前一直都在用file_get_content函数,其实早就知道有curl这么一个好东西的存在,但是看了一眼后感觉使用颇有些复杂,没有file_get_content那么简单,再就是需求也不大,所以没有学习使用curl。...

    php中file_get_content 和curl以及fopen 效率分析

    curl多用于互联网网页之间的抓取,fopen多用于读取文件,而file_get_contents多用于获取静态页面的内容。 1. fopen /file_get_contents 每次请求都会重新做DNS查询,并不对DNS信息进行缓存。但是CURL会自动对DNS信息...

    php中file_get_contents与curl性能比较分析

    在php中如果不仔细的去分析性能会发现file_get_contents与curl两个同很多共同点的,他们都可以采集文件打开文件,但是如果仔细一对比会发现很多不同点,下面我们一起来看看file_get_contents与curl区别。 PHP中fopen...

    PHP CURL或file_get_contents获取网页标题的代码及两者效率的稳定性问题

    PHP CURL与file_get_contents函数都可以获取远程服务器上的文件保存到本地,但在性能上面两者完全不在同一个级别,下面我先来介绍PHP CURL或file_get_contents函数应用例子,然后再简单的给各位介绍一下它们的一些...

    php采用file_get_contents代替使用curl实例

    file_get_contents代替使用curl其实不多见了,但有时你碰到服务器不支持curl时我们可以使用file_get_contents代替使用curl,下面看个例子。 当用尽一切办法发现 服务器真的无法使用curl时。或者curl不支持https时。...

    file_get_contents获取不到网页内容的解决方法

    $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); //在需要用户检测的网页里需要增加...

    php中curl和file_get_content的区别

    直到最近,要做一个网页小偷程序的时候才发现file_get_content已经完全不能满足需求了。我觉得,在读取远程内容的时候,file_get_content除了使用比curl便捷以外,其他都没有curl好。 主要区别: 学习才发现,curl...

    解析PHP中的file_get_contents获取远程页面乱码的问题

    PHP的file_get_contents获取远程页面内容,如果是gzip编码过的,返回的字符串就是编码后的乱码1、解决方法,找个ungzip的函数来转换下2、给你的url加个前缀,这样调用$content = file_get_contents&#40;“compress....

Global site tag (gtag.js) - Google Analytics