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 = 'http://www.111cn.net'; function get_http_status_code($url) { if(empty($url)) return false; $url = parse_url($url); $host = isset($url['host']) ? $url['host'] : ''; $port = isset($url['port']) ? $url['port'] : '80'; $path = isset($url['path']) ? $url['path'] : ''; $query = isset($url['query']) ? $url['query'] : ''; $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(' ', socket_read($socket, 1024)); socket_close($socket); return trim($response[1]); } echo get_http_status_code($url);
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!