PHP 使用 cURL 获取 HTTP 状态码和内容示例
function gethttp($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); // 获取内容url curl_setopt($curl, CURLOPT_HEADER, 1); // 获取http头信息 curl_setopt($curl, CURLOPT_NOBODY, 1); // 不返回html的body信息 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 返回数据流,不直接输出 curl_setopt($curl, CURLOPT_TIMEOUT, 9); // 超时时长,单位秒 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 自动跟踪重定向 $response = curl_exec($curl); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); $status = '' . $httpCode; echo $status; $html = file_get_contents($url); preg_match($search, $html, $Date); if($Date[1]){ return $Date[1]; }else{ return '未知'; } }
原文地址: http://www.cveoy.top/t/topic/bjIB 著作权归作者所有。请勿转载和采集!