技術(shù)頻道導(dǎo)航
HTML/CSS
.NET技術(shù)
IIS技術(shù)
PHP技術(shù)
Js/JQuery
Photoshop
Fireworks
服務(wù)器技術(shù)
操作系統(tǒng)
網(wǎng)站運營

贊助商

分類目錄

贊助商

最新文章

搜索

怎樣讀取stream_get_meta_data各項目數(shù)據(jù)

作者:admin    時間:2013-7-19 18:11:43    瀏覽:

使用stream_get_meta_data()函數(shù),可以獲得網(wǎng)頁的各meta項目信息,其中就包括有header的信息,事實上,使用stream_get_meta_data比header函數(shù)獲得的信息量更加豐富更加多,為網(wǎng)站開發(fā)提供很好的輔助作用。

不過當(dāng)我第一次接觸到stream_get_meta_data時,先是一愣,不是因為它信息量大,而是對返回的數(shù)據(jù)格式表示迷惑。我們先來看看stream_get_meta_data的返回原始數(shù)據(jù)格式。

源代碼:

$thisurl = "http://www.howtostagehomes.com/";
$fp = fopen($thisurl, 'r');
print_r(stream_get_meta_data($fp));

返回結(jié)果如下:

Array
(
    [wrapper_data] => Array
        (
            [0] => HTTP/1.1 200 OK
            [1] => Cache-Control: max-age=86400
            [2] => Content-Length: 76083
            [3] => Content-Type: text/html
            [4] => Content-Location: http://www.howtostagehomes.com/index.html
            [5] => Last-Modified: Fri, 19 Jul 2013 09:55:11 GMT
            [6] => Accept-Ranges: bytes
            [7] => ETag: "94f53df6684ce1:5cb3"
            [8] => Server: Microsoft-IIS/6.0
            [9] => X-Powered-By: ASP.NET
            [10] => Date: Fri, 19 Jul 2013 10:03:56 GMT
            [11] => Connection: close
        )
    [wrapper_type] => http
    [stream_type] => tcp_socket
    [mode] => r+
    [unread_bytes] => 1087
    [seekable] =>
    [uri] => http://www.howtostagehomes.com/
    [timed_out] =>
    [blocked] => 1
    [eof] =>
)

輸出格式是Array,我們要轉(zhuǎn)換,才可以獲得各個項目的信息,并個性化顯示在網(wǎng)頁上。

經(jīng)過測試,如下方法有效:

<?php
$url = "http://www.howtostagehomes.com/";
if(!($fp = @fopen($url, 'r')))
         return NULL;
      $meta = stream_get_meta_data($fp);
          foreach(array_keys($meta) as $h){
              $v = $meta[$h];
              echo "".$h.": ".$v."<br/>";
              if(is_array($v)){
                  foreach(array_keys($v) as $hh){
                      $vv = $v[$hh];
                      echo "".$hh.": ".$vv."<br/>";
                  }
              }
          }
      fclose($fp);
?>

經(jīng)過如上方法的處理,輸出的結(jié)果就可以更加容易被調(diào)用。結(jié)果如下:

wrapper_data: Array
0: HTTP/1.1 200 OK
1: Cache-Control: max-age=86400
2: Content-Length: 76083
3: Content-Type: text/html
4: Content-Location: http://www.howtostagehomes.com/index.html
5: Last-Modified: Fri, 19 Jul 2013 09:55:11 GMT
6: Accept-Ranges: bytes
7: ETag: "94f53df6684ce1:5cb3"
8: Server: Microsoft-IIS/6.0
9: X-Powered-By: ASP.NET
10: Date: Fri, 19 Jul 2013 10:24:21 GMT
11: Connection: close
wrapper_type: http
stream_type: tcp_socket
mode: r+
unread_bytes: 1087
seekable:
uri: http://www.howtostagehomes.com/
timed_out:
blocked: 1
eof:

補充資料

stream_get_meta_data — 從封裝協(xié)議文件指針中取得報頭/元數(shù)據(jù)

array stream_get_meta_data ( int $fp )
返回現(xiàn)有 stream 的信息。可以是任何通過 fopen(), fsockopen() 和 pfsockopen() 建立的流。返回的數(shù)組包含以下項目:

timed_out (bool) - 如果在上次調(diào)用 fread() 或者 fgets() 中等待數(shù)據(jù)時流超時了則為 TRUE。

blocked (bool) - 如果流處于阻塞 IO 模式時為 TRUE。參見 stream_set_blocking()。

eof (bool) - 如果流到達文件末尾時為 TRUE。注意對于 socket 流甚至當(dāng) unread_bytes 為非零值時也可以為 TRUE。要測定是否有更多數(shù)據(jù)可讀,用 feof() 替代讀取本項目的值。

unread_bytes (int) - 當(dāng)前在 PHP 自己的內(nèi)部緩沖區(qū)中的字節(jié)數(shù)。

相關(guān)文章
    x
    • 站長推薦
    /* 左側(cè)顯示文章內(nèi)容目錄 */