1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > contents获取网页标题的代码及两者效率的稳定性问题

contents获取网页标题的代码及两者效率的稳定性问题

时间:2018-09-17 08:16:04

相关推荐

contents获取网页标题的代码及两者效率的稳定性问题

php教程|php手册

curl_get_contents,php_curl效率

php教程-php手册

opengl球体源码,ubuntu1604共享,爬虫 图虫视频,php datafile,seo全站诊断lzw

PHP CURL与file_get_contents函数都可以获取远程服务器上的文件保存到本地,但在性能上面两者完全不在同一个级别,下面通过一个例子给大家介绍PHP CURL或file_get

动漫手机app源码,vscode插件语法高亮,ubuntu 设置时间,tomcat 上传端口,sqlite数据库名称,js与前端框架有什么关系,济南看昆虫爬虫的地方,php 10天,seo博客推广排名,css 实现网站变暗,网页侧面弹出抽屉效果,如何修改自定义表单模板lzw

jsp购物商场源码,ubuntu最近安装软件,慕课视频爬虫,引入外部php,酷狗seolzw

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

推荐方法 CURL获取

<?php$c = curl_init();$url = \;curl_setopt($c, CURLOPT_URL, $url);curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);$data = curl_exec($c);curl_close($c);$pos = strpos($data,utf-8);if($pos===false){$data = iconv("gbk","utf-8",$data);}preg_match("/

(.*)/i”,$data, $title);<p>echo $title[1];<p>?><p>使用file_get_contents</p><p><?php$content=file_get_contents("/");$pos = strpos($content,utf-8);if($pos===false){$content = iconv("gbk","utf-8",$content);}$postb=strpos($content,</p><p><title>‘)+7;<p>$poste=strpos($content,’‘);

$length=$poste-$postb;

echo substr($content,$postb,$length);

?>

看看file_get_contents性能

1)fopen/file_get_contents 每次请求远程URL中的数据都会重新做DNS查询,并不对DNS信息进行缓存。但是CURL会自动对DNS信息进行缓存。对同一域名下的网页或者图片的请求只需要一次DNS 查询。这大大减少了DNS查询的次数。所以CURL的性能比fopen/file_get_contents 好很多。

2)fopen/file_get_contents在请求HTTP时,使用的是http_fopen_wrapper,不会keeplive。而curl却可以。这样在多次请求多个链接时,curl效率会好一些。(设置header头应该可以)

3)fopen/file_get_contents函数会受到php.ini文件中allow_url_open选项配置的影响。如果该配置关闭了,则该函数也就失效了。而curl不受该配置的影响。

4)curl可以模拟多种请求,例如:POST数据,表单提交等,用户可以按照自己的需求来定制请求。而fopen/file_get_contents只能使用get方式获取数据。

5)fopen/file_get_contents 不能正确下载二进制文件

6)fopen/file_get_contents 不能正确处理ssl请求

7)curl 可以利用多线程

8)使用 file_get_contents 的时候如果 网络出现问题, 很容易堆积一些进程在这里

9)如果是要打一个持续连接,多次请求多个页面。那么file_get_contents就会出问题。取得的内容也可能会不对。所以做一些类似采集工作的时候,肯定就有问题了。对做采集抓取的用curl,如果还有同不相信下面我们再做个测试

curl与file_get_contents性能对比PHP源代码如下:

1829.php

code==’1′){

return false;

}

$city = $ipinfo->data->region.$ipinfo->data->city;

return $city;

}

function getCity($ip)

{

$url=”/service/getIpInfo.php?ip=”.$ip;

$ipinfo=json_decode(file_get_contents($url));

if($ipinfo->code==’1′){

return false;

}

$city = $ipinfo->data->region.$ipinfo->data->city;

return $city;

}

// for file_get_contents

$startTime=explode(‘ ‘,microtime());

$startTime=$startTime[0] + $startTime[1];

for($i=1;$i<=10;$i++){ echo getCity("121.207.247.202")."“;

}

$endTime = explode(‘ ‘,microtime());

$endTime = $endTime[0] + $endTime[1];

$totalTime = $endTime – $startTime;

echo ‘file_get_contents:’.number_format($totalTime, 10, ‘.’, “”).” seconds“;

//for curl

$startTime2=explode(‘ ‘,microtime());

$startTime2=$startTime2[0] + $startTime2[1];

for($i=1;$i<=10;$i++){ echo getCityCurl(121.207.247.202)."“;

}

$endTime2 = explode(‘ ‘,microtime());

$endTime2=$endTime2[0] + $endTime2[1];

$totalTime2 = $endTime2 – $startTime2;

echo “curl:”.number_format($totalTime2, 10, ‘.’, “”).” seconds”;

?>

测试访问

file_get_contents速度:4.2404510975 seconds

curl速度:2.8205530643 seconds

curl比file_get_contents速度快了30%左右,最重要的是服务器负载更低.

ps:php函数file_get_contents与curl效率及稳定性问题

习惯了使用方便快捷的file_get_contents函数抓取别家网站内容,但是总是会遇到获取失败的问题,尽管按照手册中的例子设置了超时,可多数时候不好使:

$config[‘context’] = stream_context_create(array(‘http’ => array(‘method’ => “GET”,’timeout’ => 5)));

‘timeout’ => 5//这个超时时间不稳定,经常不好使。这时候,看一下服务器的连接池,会发现一堆类似下面的错误,让你头疼万分:

file_get_contents(***): failed to open stream…

不得已,安装了curl库,写了一个函数替换:

function curl_get_contents($url)

{

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url); //设置访问的url地址

//curl_setopt($ch,CURLOPT_HEADER,1); //是否显示头部信息

curl_setopt($ch, CURLOPT_TIMEOUT, 5); //设置超时

curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_); //用户访问代理 User-Agent

curl_setopt($ch, CURLOPT_REFERER,_REFERER_); //设置 referer

curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); //跟踪301

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回结果

$r = curl_exec($ch);

curl_close($ch);

return $r;

}

如此,除了真正的网络问题外,没再出现任何问题。

这是别人做过的关于curl和file_get_contents的测试:

file_get_contents抓取需用秒数:

2.31319094

2.30374217

2.21512604

3.30553889

2.30124092

curl使用的时间:

0.68719101

0.64675593

0.64326

0.81983113

0.63956594

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。