获取正确的http状态代码php
当我访问谷歌时,我的代码给了我http状态代码302,但当我访问firefox浏览器并用firebug检查状态时,状态是200 这是我的密码:获取正确的http状态代码php,php,curl,http-headers,http-status-codes,get-headers,Php,Curl,Http Headers,Http Status Codes,Get Headers,当我访问谷歌时,我的代码给了我http状态代码302,但当我访问firefox浏览器并用firebug检查状态时,状态是200 这是我的密码: $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, T
$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);
我也尝试过获取_标题,但它给了我301状态
get_headers($url);
编辑:结果如下
array (size=26)
'url' => string 'https://www.google.com.cy/?gws_rd=cr&ei=SIeOUo7bGuKH0AWO14CYDQ' (length=62)
'content_type' => string 'text/html; charset=UTF-8' (length=24)
'http_code' => int 302
'header_size' => int 1673
'request_size' => int 317
'filetime' => int -1
'ssl_verify_result' => int 0
'redirect_count' => int 2
'total_time' => float 0.561
'namelookup_time' => float 0
'connect_time' => float 0.078
'pretransfer_time' => float 0
'size_upload' => float 0
'size_download' => float 0
'speed_download' => float 0
'speed_upload' => float 0
'download_content_length' => float 0
'upload_content_length' => float 0
'starttransfer_time' => float 0
'redirect_time' => float 0.405
'certinfo' =>
array (size=0)
empty
'primary_ip' => string '173.194.112.247' (length=15)
'primary_port' => int 443
'local_ip' => string '192.168.2.3' (length=11)
'local_port' => int 55015
'redirect_url' => string '' (length=0)
你在谷歌使用哪个网址 我使用了上面的代码,设置:
$url = 'http://www.google.com';
在curl info中收到了200条回复
我正在测试的完整代码:
奇怪-我正在使用:
$url = 'http://www.google.com';
$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_转储输出返回:
数组(26){“url”=>string(61)”“[“内容类型”]=>string(24)”text/html;字符集=UTF-8”[“http代码”]=>int(200)[“头大小”=>int(2462)[“请求大小”=>int(493)[“文件时间”]=>int(-1)[“ssl验证结果”=>int(0)[“重定向计数”]=>int(2)[“总时间”=>int(2)[“请求大小”=>int(493)[“文件时间”=>[“连接时间”]=>float(0.011754)[“预传输时间”]=>float(0.082954)[“大小上传”]=>float(0)[“大小下载”]=>float(119772)[“速度下载”]=>float(418252)[“速度上传”]=>float(0)[“下载内容长度”]=>float(262)[“上传内容长度”]=>float(0)[“开始传输时间”]=>float(0.156201)[](0.076769)[“certinfo”]=>array(0){}[“primary_-ip”]=>string(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)”}Ahh!谷歌正在通过GEO-ip或类似方式定位你,并将你重定向到本地谷歌镜像 见: 因此,当他们重定向你时,302代码是正确的 请尝试使用URL:
(ncr代表No Country Redirect)看看你是怎么做的。这些标题是关于你的cURL请求还是你的页面输出?@Scuzzy我不理解你的问题。我向google.com发出http请求,它会给我这个http状态码。请参阅修改后的答案(基于你的结果)即使没有国家重定向,我也得到了相同的状态代码。但是我使用了一个在线PHP编译器,我的代码返回了200个状态代码。所以问题是我的国家:谢谢你的尝试。我将把302个状态看作成功。