Home > Backend Development > PHP Problem > php opens other websites to get status codes

php opens other websites to get status codes

藏色散人
Release: 2023-02-28 08:08:01
Original
3643 people have browsed it

php opens other websites to get status codes

php Open other websites to get status codes?

php Get http status code program code

It is often necessary to determine whether the file can be accessed. You can judge it by the http status code. 200 means normal access, 404 means the page cannot be found. The code is as follows

<?php
// 设置url
$url = &#39;http://www.111cn.net&#39;;
function get_http_status_code($url) {
 if(empty($url)) return false;
 $url = parse_url($url);
 $host = isset($url[&#39;host&#39;]) ? $url[&#39;host&#39;] : &#39;&#39;;
 $port = isset($url[&#39;port&#39;]) ? $url[&#39;port&#39;] : &#39;80&#39;;
 $path = isset($url[&#39;path&#39;]) ? $url[&#39;path&#39;] : &#39;&#39;;
 $query = isset($url[&#39;query&#39;]) ? $url[&#39;query&#39;] : &#39;&#39;;
 $request = "HEAD $path?$query HTTP/1.1rn"
           ."Host: $hostrn"
           ."Connection: closern"
           ."rn";
 $address = gethostbyname($host);
 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
 socket_connect($socket, $address, $port);
 socket_write($socket, $request, strlen($request));
 $response = split(&#39; &#39;, socket_read($socket, 1024));
 socket_close($socket);
 return  trim($response[1]);
}
echo get_http_status_code($url);
Copy after login

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of php opens other websites to get status codes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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