PHP: How to Read the HTTP Web Page Status Code
?
How can you get remote access through a script to site pages, find out if a page exists, is it a redirect, is the link broken? This problem is extremely important when writing a script for the Web.
Here is the function defined to get this status code based on the PHP function fsockopen compatible with current versions of PHP. A script is provided showing its use, which meets strict rules.
Use fsockopen
A function with two parameters is used:
- URL of the site without a filename, for example: www.iqlevsha.ru.
- is the name of the file, possibly with a directory.
When the function is called, the domain is converted to a parameter.
$server = "www.example.com"
$fp=fsockopen($server,80,$errno,$errstr,30);
Without adding a protocol, the function uses TCP by default. You can specify tcp ://www.scribtol.ru or udp ://www.scartol.fr.
The second parameter is port, usually 80, the next two are used to get the number and report a possible error. Last is the maximum timeout allowed to receive a response in milliseconds.
After the connection is opened, a query is executed. In our case, we are talking about access to the file, hence the GET command:
$page = "index.php"
$out="GET /$page HTTP/1.1\r\n";
$out.="Host: $server\r\n";
$out.="Connection: Close\r\n\r\n";
fwrite($fp,$out);
Specify the page, server and close the connection. But the file is obtained in the same way as HTTP headers .
To restore this data, use the fgets function:
$content=fgets($fp);
The result is a line of form:
HTTP/1.1 200 Ok
if the file is found. If the URL is a redirect, it will be:
HTTP/1.1 301 Moved Permanently
While code 404 indicates that the page does not exist.
All that remains is to extract the code with the substr function, as is done in this script below.
Source code
The script source code has been tested on this site. Replace "www.iqlevsha.ru" with the URL of the site (without/at the end or filename, but optionally with tcp ://protocol).
Using the same method, replace the page names with those of the site with or without the directory.
- Source code. Zip archive.
See also