当前位置: 编程技术>php
php获取http状态示例代码
来源: 互联网 发布时间:2014-08-30
本文导语: 远程的URL地址是否能访问正常,判断其正常与否后进行下一步的操作,那么在PHP中如何获取远程HTTP的状态呢? 两种方式,大家可以自行参考/使用: #方式一 代码示例: $ch = curl_init('http://www.'); curl_setopt($ch, CURLOPT_RETURNTRANSFER...
远程的URL地址是否能访问正常,判断其正常与否后进行下一步的操作,那么在PHP中如何获取远程HTTP的状态呢?
两种方式,大家可以自行参考/使用:
#方式一
代码示例:
$ch = curl_init('http://www.');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE); // 200
curl_close($ch);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE); // 200
curl_close($ch);
#方式二
代码示例:
print_r(
get_headers('http://www.baidu.com')
);
#返回以下内容:
/*
Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sun, 04 May 2014 03:43:04 GMT
[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-37 23:55:55 GMT; 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=" OTI DSP COR IVA OUR IND COM "
[9] => Cache-Control: private
[10] => Expires: Sun, 04 May 2014 03:42:09 GMT
[11] => X-Powered-By: HPHP
[12] => Server: BWS/1.1
[13] => BDPAGETYPE: 1
[14] => BDQID: 0x9acb602d00001922
[15] => BDUSERID: 0
)
*/
get_headers('http://www.baidu.com')
);
#返回以下内容:
/*
Array
(
[0] => HTTP/1.1 200 OK
[1] => Date: Sun, 04 May 2014 03:43:04 GMT
[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-37 23:55:55 GMT; 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=" OTI DSP COR IVA OUR IND COM "
[9] => Cache-Control: private
[10] => Expires: Sun, 04 May 2014 03:42:09 GMT
[11] => X-Powered-By: HPHP
[12] => Server: BWS/1.1
[13] => BDPAGETYPE: 1
[14] => BDQID: 0x9acb602d00001922
[15] => BDUSERID: 0
)
*/
您可能感兴趣的文章:
- php获取http内容的函数示例
- HTTP中Get与Post的区别有哪些
- php获取http头信息的函数实例
- HTTP消息头网页缓存控制及header常用指令实例分享
- php(http协议)文件下载的实现代码
- php http协议应用的小例子
- php获取http请示的头信息的方法
- php获取http请求的头信息的方法