温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php判断远程资源是否存在

发布时间:2020-06-07 13:20:41 来源:网络 阅读:1050 作者:hgditren 栏目:web开发
<?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);


返回结果:

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状态信息组成的一维数组.且第一个数组单元为响应状态信息.


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

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

这里需要注意get请求https时,需要关闭证书校验的问题

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI