您现在的位置是:主页 > news > 宁波做亚马逊网站/semir

宁波做亚马逊网站/semir

admin2025/4/26 15:45:00news

简介宁波做亚马逊网站,semir,强大的技术团队网站建设,哪里有个人品牌营销公司这几天在做抓取.发现用PHP的file_get_contents函数来获取远程文件的过程中总是出现失败,并且效率很低下.所以就做了个测试的demo来测试下PHP中各种方法获取文件的速度. 程序里面使用了四种方法 分别是 1,使用输入输出缓冲和include包含远程文件拿到对应url的内容 这个需要开启…

宁波做亚马逊网站,semir,强大的技术团队网站建设,哪里有个人品牌营销公司这几天在做抓取.发现用PHP的file_get_contents函数来获取远程文件的过程中总是出现失败,并且效率很低下.所以就做了个测试的demo来测试下PHP中各种方法获取文件的速度. 程序里面使用了四种方法 分别是 1,使用输入输出缓冲和include包含远程文件拿到对应url的内容 这个需要开启…

这几天在做抓取.发现用PHP的file_get_contents函数来获取远程文件的过程中总是出现失败,并且效率很低下.所以就做了个测试的demo来测试下PHP中各种方法获取文件的速度.

程序里面使用了四种方法   分别是

1,使用输入输出缓冲和include包含远程文件拿到对应url的内容

这个需要开启PHP的allow_url_include选项

2,使用fopen来以只读的方式打开并读取远程文件.

3,使用file_get_contents函数来获取远程url文件.

4,使用PHP的curl拓展来获取远程文件.

具体里面是啥工作原理我不知道,不过通过测试我得到的结果是

第100次调用:get_file_by_curl:used_time ::::0.0732s
100次平均时间:0.084043
失败次数:0

 

第100次调用:get_file_by_file_get_contents:used_time  ::::0.103s
100次平均时间:0.11445643564356
失败次数:0

 

 

第100次调用:get_file_by_fopen:used_time  ::::0.0905s
100次平均时间:0.086212871287129
失败次数:0

 

 

第100次调用:get_file_by_include:used_time  ::::0.1248s
100次平均时间:0.11332079207921
失败次数:0

 

这上面是通过100次请求博客园首页的文件得出的结果  数据不多,但是还是能看出来区别的  用file_get_contents和include+缓冲区这两种方法的速度明显要比curl和fopen两种方式慢

下面是测试代码

<?php
/**
*名称:远程获取文件测试
*作用:测试各种方法获取远程文件的速度
*作者:swordphp@126.com
*创建时间:2013-08-29
*最后修改时间:2013-08-29
**/
ini_set("max_execution_time", "0");
set_time_limit(0);
ini_set("error_reporting", "E_ALL & ~E_NOTICE");
ini_set("allow_url_include",1);
class fileget_test{public function __construct(){}//通过CURL拓展获取文件内容public function get_file_by_curl($url){echo "get_file_by_curl:used_time";$start_time = microtime(true);$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);//设置curl地址curl_setopt($ch, CURLOPT_TIMEOUT, 5);//设置超时时间.curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);curl_setopt($ch, CURLOPT_REFERER,_REFERER_);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$content = curl_exec($ch);curl_close($ch);$used_time = round((microtime(true)-$start_time),4);echo nl2br("&nbsp::::<span style=\"color:red\";>".$used_time."s\n\r</span>");return $used_time;}//通过file_get_content来获取文件内容public function get_file_by_file_get_contents($url){echo "get_file_by_file_get_contents:used_time";$start_time = microtime(true);$content = file_get_contents($url);$used_time = round((microtime(true)-$start_time),4);echo nl2br("&nbsp ::::<span style=\"color:red\";>".$used_time."s\n\r</span>");return $used_time;}//通过fopen来获取文件内容public function get_file_by_fopen($url){echo "get_file_by_fopen:used_time";$start_time = microtime(true);$handle = fopen($url,'r');$content = fread($handle, 100000);fclose($handle);$used_time = round((microtime(true)-$start_time),4);echo nl2br("&nbsp ::::<span style=\"color:red\";>".$used_time."s\n\r</span>");return $used_time;}//通过include获取远程文件public function get_file_by_include($url){echo "get_file_by_include:used_time";$start_time = microtime(true);ob_start();include($url);$coutent = ob_get_contents();ob_clean();$used_time = round((microtime(true)-$start_time),4);echo nl2br("&nbsp ::::<span style=\"color:red\";>".$used_time."s\n\r</span>");return $used_time;}
}
function my_test($function,$url){$res = array();$test = new fileget_test;switch ($function) {case 'get_file_by_curl':for($i=1;$i<=100;$i++){echo "第".$i."次调用:";$res[$i] = $test ->get_file_by_curl($url);}echo nl2br("100次平均时间:".array_sum($res)/count($res)."\n\r");echo "失败次数:".count(array_keys($res,false));break;case 'get_file_by_file_get_contents':for($i=0;$i<=100;$i++){echo "第".$i."次调用:";$res[$i] = $test ->get_file_by_file_get_contents($url);}echo nl2br("100次平均时间:".array_sum($res)/count($res)."\n\r");echo "失败次数:".count(array_keys($res,false));break;case 'get_file_by_fopen':for($i=0;$i<=100;$i++){echo "第".$i."次调用:";$res[$i] = $test ->get_file_by_fopen($url);}echo nl2br("100次平均时间:".array_sum($res)/count($res)."\n\r");echo "失败次数:".count(array_keys($res,false));break;case 'get_file_by_include':for($i=0;$i<=100;$i++){echo "第".$i."次调用:";$res[$i] = $test ->get_file_by_include($url);}echo nl2br("100次平均时间:".array_sum($res)/count($res)."\n\r");echo "失败次数:".count(array_keys($res,false));break;default:echo "no function selected!";break;}}
$function = $_GET['f'];
$url = isset($_GET['url'])?$_GET['url']:'http://www.taobao.com';
my_test($function,$url);

  这个测试的结果还不能够说明什么,我打算再找机会好好测试下.这里我有几个地方不太理解,之前有人也做过类似的测试 只测试了file_get_content和curl,明显是后者快一些.

 

转载于:https://www.cnblogs.com/swordphp/p/3289965.html