How does php determine the status of http
Editor to share with you how to judge the status of php http, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
Php's method to judge the state of http: [header ("HTTP/1.1 404 Not Found"); $url= "http://www.xxxx.com/preg.php"; $ch = curl_init (); curl_seto...].
This article operating environment: windows10 system, php 7, thinkpad T480 computer.
In the actual project development, we sometimes need to know whether the remote URL address can be accessed properly, and decide whether to proceed to the next step by judging whether it is normal or not. So how do we judge the status of HTTP? In fact, it is not difficult, we can take the following two ways, and then let's take a look at these two ways.
File preg.php
Header ("HTTP/1.1 404 Not Found")
The 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 bodycurl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE); $head = curl_exec ($ch); $httpCode = curl_getinfo ($ch, CURLINFO_HTTP_CODE); echo $httpCode;curl_close ($ch)
Running result:
four hundred and four
The second method:
Echo'; print_r (get_headers ("http://www.demo.com/preg.php",1));
Running result:
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) these are all the contents of the article "how php judges http status". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!