时间:2021-07-01 10:21:17 帮助过:22人阅读
php打开其他网站获取状态码?
php获取http状态码程序代码
经常需要判断文件是否可以访问,可以通过http状态码判别,200为正常访问,404为找不到该页面,代码如下
<?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);
更多PHP相关知识,请访问PHP中文网!
以上就是php打开其他网站获取状态码的详细内容,更多请关注Gxl网其它相关文章!