當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP get_headers()用法及代碼示例


PHP中的get_headers()函數用於獲取服務器在HTTP請求的響應中發送的所有標頭。

用法:

get_headers( $url, $format, $context )

參數:此函數接受上述和以下所述的三個參數:


  • $url:它是字符串類型的必需參數。它定義了目標URL。
  • $format:它是int類型的可選參數。如果其值設置為非零,它將返回一個關聯數組,否則為索引數組。
  • $context:它保存由stream_context_create()函數創建的有效資源上下文。

範例1:在此示例中,未分配可選參數$format的值。

<?php 
  
// Target URL 
$url = "https://www.geeksforgeeks.org"; 
  
// Fetching headers 
$headers = get_headers($url); 
  
// Printing headers 
print_r($headers); 
?>

輸出:

Array (
     [0] => HTTP/1.1 200 OK 
     [1] => Content-Type: text/html; charset=UTF-8 
     [2] => Connection: close 
     [3] => Date: Sun, 19 May 2019 08:31:29 GMT 
     [4] => Server: Apache 
     [5] => Strict-Transport-Security: max-age=3600; includeSubDomains 
     [6] => Cache-Control: s-maxage=21600, max-age=3, must-revalidate 
     [7] => Access-Control-Allow-Credentials: true 
     [8] => X-Frame-Options: DENY 
     [9] => X-Content-Type-Options: nosniff 
     [10] => Vary: Accept-Encoding, Cookie 
     [11] => X-Cache: Miss from cloudfront 
     [12] => Via: 1.1 aa0bb866c09b4e243eb9a97bcdb7fe32.cloudfront.net (CloudFront) 
     [13] => X-Amz-Cf-Id: QAOIIj4eBsrX0hyZ-UHjOtqA2dQePcLbEUZJ3KRohjsSPfcrcAFaiQ== 
) 

範例2:在此示例中,可選參數$format的值設置為非零。

<?php 
  
// Target URL 
$url = "https://www.geeksforgeeks.org"; 
  
// Fetching headers 
$headers = get_headers($url, 1); 
  
// Printing headers 
print_r($headers); 
?>

輸出:

Array ( 
        [0] => HTTP/1.1 200 OK 
        [Content-Type] => text/html; charset=UTF-8 
        [Connection] => close 
        [Date] => Sun, 19 May 2019 08:35:47 GMT 
        [Server] => Apache 
        [Strict-Transport-Security] => max-age=3600; includeSubDomains 
        [Cache-Control] => s-maxage=21600, max-age=3, must-revalidate 
        [Access-Control-Allow-Credentials] => true 
        [X-Frame-Options] => DENY 
        [X-Content-Type-Options] => nosniff 
        [Vary] => Accept-Encoding, Cookie 
        [X-Cache] => Miss from cloudfront 
        [Via] => 1.1 95d17b4d563934eb90636ad03f8f524e.cloudfront.net (CloudFront) 
        [X-Amz-Cf-Id] => se3QRyaWDeuHI3GrisMzAr4FJBamqMtbUNzhTPqAJhBoQZbWvy3UPw== 
) 


相關用法


注:本文由純淨天空篩選整理自aman neekhara大神的英文原創作品 PHP | get_headers() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。