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

推荐方法 CURL获取

 代码如下 复制代码

<?php
$c = curl_init();
$url = 'www.111cn.net';
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("/<title>(.*)<\/title>/i",$data, $title);
echo $title[1];
?>

使用file_get_contents

 代码如下 复制代码

<?php
$content=file_get_contents("http://www.111cn.net/");
$pos = strpos($content,'utf-8');
if($pos===false){$content = iconv("gbk","utf-8",$content);}
$postb=strpos($content,'<title>')+7;
$poste=strpos($content,'</title>');
$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

 代码如下 复制代码
<?php
/**
 
* 通过淘宝IP接口获取IP地理位置
 
* @param string $ip
 
* @return: string
 
**/
function getCityCurl($ip)
{
    $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
    $ch = curl_init();
    $timeout = 5;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents = curl_exec($ch);
    curl_close($ch);
 
    $ipinfo=json_decode($file_contents);
    if($ipinfo->code=='1'){
        return false;
    }
    $city = $ipinfo->data->region.$ipinfo->data->city;
    return $city;
}
 
function getCity($ip)
{
    $url="http://ip.taobao.com/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")."</br>";
}
$endTime = explode(' ',microtime());
$endTime = $endTime[0] + $endTime[1];
$totalTime = $endTime - $startTime;
echo 'file_get_contents:'.number_format($totalTime, 10, '.', "")." seconds</br>";
 
//for curl
$startTime2=explode(' ',microtime());
$startTime2=$startTime2[0] + $startTime2[1];
for($i=1;$i<=10;$i++)
{
   echo getCityCurl('121.207.247.202')."</br>";
}
$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%左右,最重要的是服务器负载更低.

时间: 2024-09-02 16:29:39

PHP CURL或file_get_contents获取网页标题的代码的相关文章

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

PHP CURL与file_get_contents函数都可以获取远程服务器上的文件保存到本地,但在性能上面两者完全不在同一个级别,下面我先来介绍PHP CURL或file_get_contents函数应用例子,然后再简单的给各位介绍一下它们的一些小区别吧. 推荐方法 CURL获取 <?php $c = curl_init(); $url = 'www.jb51.net'; curl_setopt($c, CURLOPT_URL, $url); curl_setopt($c, CURLOPT_R

PHP获取网页标题的3种实现方法代码实例

 这篇文章主要介绍了PHP获取网页标题的3种实现方法,分别使用CURL.file()函数.file_get_contents实现,需要的朋友可以参考下 一.推荐方法 CURL获取   <?php $c = curl_init(); $url = 'www.jb51.net'; curl_setopt($c, CURLOPT_URL, $url); curl_setopt($c, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($c); curl_clo

javascript 获取网页标题代码实例

 这篇文章主要介绍了javascript 获取网页标题代码实例,有需要的朋友可以参考一下 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"&

javascript 获取网页标题代码实例_javascript技巧

复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="

javascript_获取网页标题代码

javascript_获取网页标题代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.jzread.com/1999/xhtml"> <head> <meta h

php获取网页标题和内容函数

有时候我们需要获取网页的标题与内容,就是个采集函数,这里简单分享下,方便需要的朋友  代码如下: function getPageContent($url) {              //$url='http://www.ttphp.com;              $pageinfo = array();            $pageinfo[content_type] = '';            $pageinfo[charset] = '';            $pag

php中file_get_contents获取网页乱码解决办法

我采集的一个页面,如下gzip 知道原因了我们就好办了,先百度了一下得出是可以改用curl操作. curl解决  代码如下 复制代码 function curl_get($url, $gzip=false){         $curl = curl_init($url);         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);         curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);  

php获取网页标题和内容函数(不包含html标签)_php技巧

复制代码 代码如下: function getPageContent($url) {           //$url='http://www.ttphp.com;           $pageinfo = array();           $pageinfo[content_type] = '';           $pageinfo[charset] = '';           $pageinfo[title] = '';           $pageinfo[descript

asp.net中动态改变网页标题的代码

方法1. 首先:在.aspx页: <HEAD> <title> <%=PageTitle %> </title> ....... </HEAD> 其次:在.aspx.cs页: public class news_view : System.Web.UI.Page { ............ //用于动态设置页面标题 protected string PageTitle; .... private void Page_Load(object se