溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

php判斷遠(yuǎn)程資源是否存在

發(fā)布時(shí)間:2020-06-07 13:20:41 來源:網(wǎng)絡(luò) 閱讀:1021 作者:hgditren 欄目:web開發(fā)
<?php
$url1='https://cache.yisu.com/upload/information/20200310/52/107771.jpg'?//exists
$url2='https://cache.yisu.com/upload/information/20200310/52/107776.jpg';?//not?exists?



$get1=get_headers($url1);
$get2=get_headers($url2);

print_r($get1);
print_r($get2);


返回結(jié)果:

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:?p_w_picpath/png
)

可以看到返回的http狀態(tài)信息組成的一維數(shù)組.且第一個(gè)數(shù)組單元為響應(yīng)狀態(tài)信息.


于是乎,通過獲取到的狀態(tài)碼,用來判斷遠(yuǎn)程資源是否存在。

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

這里需要注意get請(qǐng)求https時(shí),需要關(guān)閉證書校驗(yàn)的問題

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI