Home  >  Article  >  Backend Development  >  How to determine http status in php

How to determine http status in php

王林
王林Original
2021-09-23 14:11:182518browse

php method to determine http status: [header("HTTP/1.1 404 Not Found"); $url="http://www.xxxx.com/preg.php"; $ch = curl_init( ); curl_seto...].

How to determine http status in php

The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.

In actual project development, we sometimes need to know whether the remote URL address can be accessed normally, and decide whether to proceed with the next step by judging whether it is normal or not. So how do we determine the status of HTTP? In fact, it is not difficult. We can take the following two methods. Next, let us take a look at these two methods.

File preg.php

header("HTTP/1.1 404 Not Found");

First method:

$url = "http://www.demo.com/preg.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo $httpCode;
curl_close($ch);

Run result:

404

Second method:

echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r(get_headers("http://www.demo.com/preg.php",1));

Running results:

Array
(
    [0] => HTTP/1.1 404 Not Found
    [Date] => Fri, 28 Sep 2018 09:27:03 GMT
    [Server] => Apache/2.4.23 (Win32) OpenSSL/1.0.2j PHP/5.5.38
    [X-Powered-By] => PHP/5.5.38
    [Content-Length] => 0
    [Content-Type] => text/html
)

Recommended learning:php training

The above is the detailed content of How to determine http status in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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