php教程

get_headers获取不到http状态码解决方法

php教程 51源码 2023-05-08 人阅读

有一个项目需要检测网站HTTP状态码来判断,我的站长站使用get_headers函数检查状态,apache环境下状态为200,但用nginx它给了我 301状态。

所以很明显get_headers在某些情况下不兼容,那么我们只能使用兼容性更高的curl 方法了,参考代码如下:

$url = 'https://www.51yma.cn';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
var_dump($info);

我收到的 var_dump 输出:

array(26) { ["url"]=> string(61) "https://www.google.co.uk/?gws_rd=cr&ei=fIaOUsflOqnG0QXy74DYDg "["content_type"]=> string(24) "text/html; charset=UTF-8"[ "http_code"]=> int(200) ["header_size"]=> int(2462) ["request_size"]=> int(493) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(2) ["total_time"]=> float(0.286363) ["namelookup_time"]=> float(7.1E-5) ["connect_time"] => float (0.011754)[“预传输时间”]=> float (0.082954)[“大小上传”]=> float (0)[“大小下载”]=> float (119772)[“速度下载”]=> float (418252) ["speed_upload"]=> float(0) ["download_content_length"]=> float(262) ["upload_content_length"]=> float(0) ["starttransfer_time"]=> float(0.156201) ["redirect_time"]= > float (0.076769)[“certinfo”]=>数组(0){}[“primary_ip”]=>字符串(14)“173.194.34.183”[“primary_port”]=> int(443)[“local_ip”] => string(12) "192.168.0.15"["local_port"]=> int(54606) ["redirect_url"]=> string(0) ""}

完美解决了Nginx不支持get_headers函数。

版权声明:文章搜集于网络,如有侵权请联系本站,转载请说明出处:https://www.51yma.cn/jiaocheng/php/1249.html
文章来源: