function get_http_code($url="localhost", $port=80, $fsock_timeout=10){ set_time_limit(0); ignore_user_abort(true); // 记录开始时间 list($usec, $sec) = explode(" ", microtime(true)); $timer['start'] = (float)$usec + (float)$sec; // 校验URL if(!preg_match("/^https?:\/\//i", $url)){ $url = "http://".$url; } // 支持HTTPS if(preg_match("/^https:\/\//i", $url)){ $port = 443; } // 理会URL $urlinfo = parse_url($url); if(empty($urlinfo['path'])){ $urlinfo['path'] = '/'; } $host = $urlinfo['host']; $uri = $urlinfo['path'] . (empty($urlinfo['query'])?'':$urlinfo['query']); // 通过fsock打开毗连 if(!$fp = fsockopen($host, $port, $errno, $error, $fsock_timeout)){ list($usec, $sec) = explode(" ", microtime(true)); $timer['end'] = (float)$usec + (float)$sec; $usetime = (float)$timer['end'] - (float)$timer['start']; return array('code'=>-1, 'usetime'=>$usetime); } // 提交请求 $status = socket_get_status($fp); $out = "GET {$uri} HTTP/1.1\r\n"; $out .= "Host: {$host}\r\n"; $out .= "Connection: Close\r\n\r\n"; $write = fwrite($fp, $out); if(!$write){ list($usec, $sec) = explode(" ", microtime(true)); $timer['end'] = (float)$usec + (float)$sec; $usetime = (float)$timer['end'] - (float)$timer['start']; return array('code'=>-2, 'usetime'=>$usetime); } $ret = fgets($fp, 1024); preg_match("/http\/\d\.\d\s(\d+)/i", $ret, $m); $code = $m[1]; fclose($fp); list($usec, $sec) = explode(" ", microtime(true)); $timer['end'] = (float)$usec + (float)$sec; $usetime = (float)$timer['end'] - (float)$timer['start']; return array('code'=>$code, 'usetime'=>$usetime); }原文地点:?post=707
php实现获取网页请求状态非get
内容版权声明:除非注明,否则皆为本站原创文章。