php請求遠程檔案之返回頭部資訊 get_headers()

來源:互聯網
上載者:User

標籤:php   header   回頭部資訊   

<?php$url1=‘ //not exists $url2=‘ //exists $get1=get_headers($url1);$get2=get_headers($url2);print_r($get1);print_r($get2);


返回結果:

Array(    [0] => HTTP/1.1 404 Not Found    [1] => Date: Tue, 14 Mar 2017 10:58:00 GMT    [2] => Server: Apache    [3] => Vary: User-Agent,Accept-Encoding    [4] => Connection: close    [5] => Content-Type: text/html)Array(    [0] => HTTP/1.1 200 OK    [1] => Date: Tue, 14 Mar 2017 10:58:00 GMT    [2] => Server: Apache    [3] => Last-Modified: Wed, 09 Nov 2016 08:57:06 GMT    [4] => ETag: "2162a05-13f5-540da70f97c80"    [5] => Accept-Ranges: bytes    [6] => Content-Length: 5109    [7] => Vary: User-Agent    [8] => Connection: close    [9] => Content-Type: image/png)可以看到返回的http狀態資訊組成的一維數組.且第一個數組單元為響應狀態資訊.


於是乎,通過擷取到的狀態代碼,用來判斷遠端資源是否存在。

$statusCode=substr($get1[0], 9, 3);if($statusCode == 200 || $statusCode == 304){    echo "It‘s exists."}else{    echo "It‘s not exists."}


本文出自 “我的PHP之路” 部落格,請務必保留此出處http://phpme.blog.51cto.com/663593/1906497

php請求遠程檔案之返回頭部資訊 get_headers()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.