php获取远程页面的meta description keywords title和响应头(header)信息

时间:2016-08-31
本文章向大家介绍php如何获取远程页面的meta description keywords title和头(header)信息,主要使用到php相关文件函数、正则表达式函数和get_meta_tags函数,需要的朋友可以参考一下。

php获取远程页面meta description keywords信息

我们可以直接使用php get_meta_tags函数获取远程页面meta description keywords信息,具体实现代码如下:

<?php
$tags = get_meta_tags('http://www.manongjc.com/');
$keywords = $tags['keywords'];
$description = $tags['description'];
$author = $tags['author'];

echo "<b>Description:</b> $description<br>";
echo "<b>Keywords:</b> $keywords<br>";
echo "<b>Author:</b> $author<br>";
?>

 

php获取远程页面title标题信息

首先,打开/读取远程页面。打开读取远程页面可以使用fopen()函数,也可以使用file_get_contents()函数,这里我们使用fopen()函数,代码如下:

<?php
$url = "http://www.manongjc.com";
$page = fopen($url, 'r');
$content = "";
while( !feof( $page ) ) {
$buffer = trim( fgets( $page, 4096 ) );
$content .= $buffer;
}
?>

接着,我们提取title标签之间的文本:

<?php
eregi('',$content,$tmp);
$result = ereg_replace('[[:blank:]]',' ',$tmp[1]);
echo "$result";
?>

 

php获取远程页面的头信息

php开发人员可以用下面的代码提取来自远程页面的title信息:

<?php
$fp = fopen('http://www.baidu.com', 'r');
// Creates variable $http_response_header
/*  http://www.manongjc.com/article/1424.html */
print_r($http_response_header);
// or
$meta_data = stream_get_meta_data($fp);
print_r($meta_data);
?>

运行结果:

Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Date: Wed, 31 Aug 2016 05:03:49 GMT
    [2] => Content-Type: text/html
    [3] => Content-Length: 14613
    [4] => Last-Modified: Tue, 23 Aug 2016 03:40:28 GMT
    [5] => Connection: Close
    [6] => Vary: Accept-Encoding
    [7] => Set-Cookie: BAIDUID=C75A224EC0342039FBDAF3CC9B0E6F8E:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
    [8] => Set-Cookie: BIDUPSID=C75A224EC0342039FBDAF3CC9B0E6F8E; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
    [9] => Set-Cookie: PSTM=1472619829; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
    [10] => P3P: CP=" OTI DSP COR IVA OUR IND COM "
    [11] => Server: BWS/1.1
    [12] => X-UA-Compatible: IE=Edge,chrome=1
    [13] => Pragma: no-cache
    [14] => Cache-control: no-cache
    [15] => Accept-Ranges: bytes
)
Array
(
    [wrapper_data] => Array
        (
            [0] => HTTP/1.1 200 OK
            [1] => Date: Wed, 31 Aug 2016 05:03:49 GMT
            [2] => Content-Type: text/html
            [3] => Content-Length: 14613
            [4] => Last-Modified: Tue, 23 Aug 2016 03:40:28 GMT
            [5] => Connection: Close
            [6] => Vary: Accept-Encoding
            [7] => Set-Cookie: BAIDUID=C75A224EC0342039FBDAF3CC9B0E6F8E:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
            [8] => Set-Cookie: BIDUPSID=C75A224EC0342039FBDAF3CC9B0E6F8E; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
            [9] => Set-Cookie: PSTM=1472619829; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
            [10] => P3P: CP=" OTI DSP COR IVA OUR IND COM "
            [11] => Server: BWS/1.1
            [12] => X-UA-Compatible: IE=Edge,chrome=1
            [13] => Pragma: no-cache
            [14] => Cache-control: no-cache
            [15] => Accept-Ranges: bytes
        )

    [wrapper_type] => http
    [stream_type] => tcp_socket/ssl
    [mode] => r
    [unread_bytes] => 0
    [seekable] => 
    [uri] => http://www.baidu.com
    [timed_out] => 
    [blocked] => 1
    [eof] => 
)

上一页 下一页

原文地址:http://www.manongjc.com/article/1424.html