iT邦幫忙

1

curl結果疑問

舜~ 2020-08-07 14:19:173096 瀏覽

各位前輩請教一下

好奇為什麼curl -i xxx 跟 curl -I 的http code 會不一樣?

舉例來說
curl -I -k https://www.twtainan.net/
這個會出現302

curl -i -k https://www.twtainan.net/
這則是200正常

大小i的差別應該只有有沒有顯示body ??

環境:
win10 cmd下輸入指令


再請教一下,若用PHP撈取上述網址的表頭的話

curl通常會這樣設定
curl_setopt($curl, CURLOPT_HEADER, true); // 顯示表頭
curl_setopt($curl, CURLOPT_NOBODY, true); // 忽略內容

但這樣的話,
curl_getinfo($curl, CURLINFO_HTTP_CODE)也是抓到會出現302,
若想讓他能抓到200,然後只顯示表頭的話,
要怎麼設定?

咖咖拉 iT邦好手 1 級 ‧ 2020-08-07 14:36:31 檢舉
curl 的說明
-i, --include Include protocol response headers in the output
-I, --head Show document info only
可能要換一種函式,或者你要檢查是status為轉址的號碼的要重新再執行curl函式
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
screenleon
iT邦新手 1 級 ‧ 2020-08-07 14:38:18

-I 跟 -i 跑的指令有些微差別

-I, --head
      (HTTP FTP FILE) Fetch the headers only! HTTP-servers feature the command HEAD which this  uses  to  get nothing but the header of a document. When used on an FTP or FILE file, curl displays the file size and last modification time only.
              
-i, --include
      Include  the  HTTP  response  headers  in the output. The HTTP response headers can include things like server name, cookies, date of the document, HTTP version and more...
      To view the request headers, consider the -v, --verbose option.

所以看起來 -i 只會輸出最後的結果, -I 只會取得最初的header資訊

我要發表回答

立即登入回答