Home > Backend Development > PHP Tutorial > PHP sample code analysis to determine whether remote resources exist

PHP sample code analysis to determine whether remote resources exist

黄舟
Release: 2023-03-06 21:04:01
Original
1840 people have browsed it

php sample code analysis to determine whether remote resources exist

<?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);
Copy after login

Return result:

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

So, the obtained status code is used to determine the remote Whether the resource exists.

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

The above is the detailed content of PHP sample code analysis to determine whether remote resources exist. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template