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打開其他網站取得狀態碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!