获取HTTP状态码的PHP方法
在PHP中,可以使用`file_get_contents()`函数获取HTTP状态码。以下是一个示例: ```php $url = "http://www.example.com"; $response = file_get_contents($url); $status_code = http_response_code(); echo "HTTP状态码: " . $status_code; ```
方式一
$ch=curl_init('https://www.caijicaiji.com');curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_exec($ch);echocurl_getinfo($ch,CURLINFO_HTTP_CODE);//200curl_close($ch);
方式二
print_r(get_headers('https://www.caijicaiji.com'));
返回以下内容:
Array([0]=>HTTP/1.1200OK[1]=>Date:Sun,04May201403:43:04GMT[2]=>Content-Type:text/html;charset=utf-8[3]=>Connection:Close[4]=>Vary:Accept-Encoding[5]=>Set-COOKIE:BAIDUID=4977AF4FB1E9A5D13C79939E28D92161:FG=1;expires=Thu,31-Dec-3723:55:55GMT;max-age=2147483647;path=/;domain=.baidu.com[6]=>Set-COOKIE:BDSVRTM=0;path=/[7]=>Set-COOKIE:H_PS_PSSID=4681_1465_5224_6023_4759_6018_6257_6313_6328_6269;path=/;domain=.baidu.com[8]=>P3P:CP="OTIDSPCORIVAOURINDCOM"[9]=>Cache-Control:private[10]=>Expires:Sun,04May201403:42:09GMT[11]=>X-Powered-By:HPHP[12]=>Server:BWS/1.1[13]=>BDPAGETYPE:1[14]=>BDQID:0x9acb602d00001922[15]=>BDUSERID:0)