php专区

 首页 > php专区 > PHP应用 > php函数大全 > PHP CURL或file_get_contents获取网页标题的代码

PHP CURL或file_get_contents获取网页标题的代码

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

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

推荐方法 CURL获取,代码如下:

  1. $c = curl_init(); 
  2. $url = 'www.phpfensi.com'
  3. curl_setopt($c, CURLOPT_URL, $url); 
  4. curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); 
  5. $data = curl_exec($c); 
  6. curl_close($c); 
  7. $pos = strpos($data,'utf-8'); 
  8. if($pos===false){$data = iconv("gbk","utf-8",$data);} 
  9. preg_match("/(.*)<\/title>/i"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;">$data</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;">$title</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span> </li> <li class="alt" style="word-wrap: break-word; list-style: decimal; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 14px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"> <span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="func" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">echo</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;">$title</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">[1]; </span></span> </li> <li style="word-wrap: break-word; list-style: decimal; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 14px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"> <span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">?> </span> </li> </ol> </blockquote> <p style="margin: 8px 0px 14px; padding: 0px; word-wrap: break-word; text-indent: 2em; color: rgb(51, 51, 51); font-family: Verdana; font-size: 14px; line-height: 25.2000007629395px;"> 使用file_get_contents,代码如下:</p> <blockquote style="margin: 0px; padding: 5px 8px; word-wrap: break-word; font-family: Consolas, 'Courier New', Courier, mono, serif; width: 605.875px; border: 1px dotted rgb(170, 170, 170); line-height: 18px; color: rgb(51, 153, 51); background: url(http://www.phpfensi.com/statics/skin/blockquote3.png) 100% 0% no-repeat rgb(250, 250, 250);"> <ol class="dp-c" style="margin-left: 4em; padding-right: 0px; padding-left: 0px; word-wrap: break-word; list-style-position: outside; list-style-image: initial; border: none; color: rgb(92, 92, 92); margin-top: 0px !important; margin-right: 0px !important; margin-bottom: 1px !important; background-color: rgb(255, 255, 255);"> <li class="alt" style="word-wrap: break-word; list-style: decimal; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 14px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"> <span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"><?php </span></span> </li> <li style="word-wrap: break-word; list-style: decimal; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 14px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"> <span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;">$content</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="func" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">file_get_contents</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"http://www.phpfensi.com/"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span> </li> <li class="alt" style="word-wrap: break-word; list-style: decimal; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 14px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"> <span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;">$pos</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> = </span><span class="func" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">strpos</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;">$content</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">'utf-8'</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">); </span></span> </li> <li style="word-wrap: break-word; list-style: decimal; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 14px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"> <span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 102, 153); font-weight: bold; background-color: inherit;">if</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;">$pos</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">===false){</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;">$content</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> = iconv(</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"gbk"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">"utf-8"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;">$content</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);} </span></span> </li> <li class="alt" style="word-wrap: break-word; list-style: decimal; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); color: inherit; line-height: 14px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"> <span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;">$postb</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="func" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">strpos</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;">$content</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">'<title>'</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">)+7; </span></span> </li> <li style="word-wrap: break-word; list-style: decimal; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(108, 226, 108); line-height: 14px; margin: 0px !important; padding: 0px 3px 0px 10px !important; background-color: rgb(248, 248, 248);"> <span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;">$poste</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">=</span><span class="func" style="margin: 0px; padding: 0px; border: none; background-color: inherit;">strpos</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">(</span><span class="vars" style="margin: 0px; padding: 0px; border: none; color: rgb(221, 0, 0); background-color: inherit;">$content</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">,</span><span class="string" style="margin: 0px; padding: 0px; border: none; color: blue; background-color: inherit;">''); 
  10. $length=$poste-$postb
  11. echo substr($content,$postb,$length); 
  12. ?> 

看看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源代码如下:

  1. /** 
  2.  
  3. * 通过淘宝IP接口获取IP地理位置 
  4.  
  5. * @param string $ip 
  6.  
  7. * @return: string 
  8.  
  9. **/ 
  10. function getCityCurl($ip
  11.     $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip
  12.     $ch = curl_init(); 
  13.     $timeout = 5; 
  14.     curl_setopt ($ch, CURLOPT_URL, $url); 
  15.     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  16.     curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
  17.     $file_contents = curl_exec($ch); 
  18.     curl_close($ch); 
  19.  
  20.     $ipinfo=json_decode($file_contents); 
  21.     if($ipinfo->code=='1'){ 
  22.         return false; 
  23.     } 
  24.     $city = $ipinfo->data->region.$ipinfo->data->city; 
  25.     return $city
  26.  
  27. function getCity($ip
  28.     $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip
  29.     $ipinfo=json_decode(file_get_contents($url)); 
  30.     if($ipinfo->code=='1'){ 
  31.         return false; 
  32.     } 
  33.     $city = $ipinfo->data->region.$ipinfo->data->city; 
  34.     return $city
  35.  
  36. // for file_get_contents 
  37. $startTime=explode(' ',microtime()); 
  38. $startTime=$startTime[0] + $startTime[1]; 
  39. for($i=1;$i<=10;$i++) 
  40.    echo getCity("121.207.247.202")."
    "
  41. $endTime = explode(' ',microtime()); 
  42. $endTime = $endTime[0] + $endTime[1]; 
  43. $totalTime = $endTime - $startTime
  44. echo 'file_get_contents:'.number_format($totalTime, 10, '.'"")." seconds
    "
  45.  
  46. //for curl 
  47. $startTime2=explode(' ',microtime()); 
  48. $startTime2=$startTime2[0] + $startTime2[1]; 
  49. for($i=1;$i<=10;$i++) 
  50. //开源软件:phpfensi.com 
  51.    echo getCityCurl('121.207.247.202')."
    "
  52. $endTime2 = explode(' ',microtime()); 
  53. $endTime2=$endTime2[0] + $endTime2[1]; 
  54. $totalTime2 = $endTime2 - $startTime2
  55. echo "curl:".number_format($totalTime2, 10, '.'"")." seconds"
  56. ?> 

测试访问:

file_get_contents速度:4.2404510975 seconds

curl速度:2.8205530643 seconds

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

分享到:
学PHP5:session会话的使用和分析
        Session的中文译名叫做“会话”,其本来的含义是指有始有终的一系列动作/消息,比如打电话时从拿起电话拨号到挂断电话这中间的一系列过程可以称之为一个session。目前社会上对session的理解非常混乱:有时候我们可以看到这样的话“在一个浏览器会话期间,...”,这里的会话是指从一个浏览器窗口打开到关闭这个...
PHP里8个鲜为人知的PHP安全函数
安全是编程非常重要的一个方面。在任何一种编程语言中,都提供了许多的函数或者模块来确保程序的安全性。在现代网站应用中,经常要获取来自世界各地用户的输入,但是,我们都知道“永远不能相信那些用户输入的数据”。所以在各种的Web开发语言中,都会提供保证用户输入数据安全的函数。今天,我们就来看看,在著名的开源语...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……