php判断远程资源是否存在的示例代码分析

黄舟
发布: 2023-03-06 21:04:01
原创
1742 人浏览过

php判断远程资源是否存在的示例代码分析

<?php
$url1=&#39;http://www.91hi.net/web/demo/1.jpg&#39; //exists
$url2=&#39;http://91hi.net/web/demo/www/jd/image/logo.png&#39;; //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: image/png
)
 
可以看到返回的http状态信息组成的一维数组.且第一个数组单元为响应状态信息.
登录后复制

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

$statusCode=substr($get1[0], 9, 3);
if($statusCode == 200 || $statusCode == 304){
    echo "It&#39;s exists."
}else{
    echo "It&#39;s not exists."
}
登录后复制

以上是php判断远程资源是否存在的示例代码分析的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!