php判断远程资源是否存在

1
2
3
4
5
6
7
8
9
10
11
<?php
$url1 = 'http://www.91hi.net/web/demo/1.jpg'  //exists
$url2 = 'http://91hi.net/web/demo/www/jd/image/logo.png' ; //not exists 
 
 
 
$get1 =get_headers( $url1 );
$get2 =get_headers( $url2 );
 
print_r( $get1 );
print_r( $get2 );


返回结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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状态信息组成的一维数组.且第一个数组单元为响应状态信息.


于是乎,通过获取到的状态码,用来判断远程资源是否存在。

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










本文转自 hgditren 51CTO博客,原文链接:http://blog.51cto.com/phpme/1906497,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值