PHP cURL獲取HTTP響應頭

2021-10-25 15:49:14 字數 831 閱讀 4745

平時做開發時,經常會用到php的curl擴充套件,用於請求外部http介面。大多數情況下,我們只需要獲取口返回的響應體(http response body),但如果我們想獲取響應頭(http response header)那可以怎麼做呢?

可惜的是,curl擴充套件沒有提供原生的方法讓我們(以陣列的形式)直接獲取響應頭,只能通過稍微複雜一點的方法來獲取。

直接上**:

<?php

function

headerhandler

($curl

,$headerline

)return

$len;}

$ch=

curl_init()

;curl_setopt

($ch

,curlopt_url

,"");

curl_setopt

($ch

,curlopt_returntransfer,1

);curl_setopt

($ch

,curlopt_headerfunction

,"headerhandler");

// 設定header處理函式

$responsebody

=curl_exec

($ch);

curl_close

($ch);

var_dump

($globals

['g_header'])

;// 以陣列形式列印響應頭

var_dump

($responsebody);

// 列印響應體

php CURL 請求頭和響應頭獲取

1.從curl中獲取響應頭 ocurl curl init 設定請求頭,有時候需要,有時候不用,看請求 是否有對應的要求 返回 response header,該選項非常重要,如果不為 true,只會獲得響應的正文 curl setopt ocurl,curlopt header,true 是否不需...

php CURL 請求頭和響應頭獲取

1.從curl中獲取響應頭 ocurl curl init 設定請求頭,有時候需要,有時候不用,看請求 是否有對應的要求 返回 response header,該選項非常重要,如果不為 true,只會獲得響應的正文 curl setopt ocurl,curlopt header,true 是否不需...

python 獲取http響應

乙個相對完整的http請求,輸入ip和埠,輸出響應碼,響應頭,響應體,是否超時,以及出錯時的錯誤資訊 處理包括 1.協議處理,如果是443用https,其他用http 2.httperror處理,httperror一般是401,403,404之類的錯誤,雖然報錯,但是也有響應頭。注意獲取錯誤資訊時要...