4

当我访问谷歌时,我的代码给了我 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, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

我也试过 get_headers 但它给了我 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)
4

2 回答 2

3

您使用哪个 URL 访问 Google?

我在上面使用了您的代码,设置:

$url = 'http://www.google.com';

并在 curl 信息中收到 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_dump 输出:

数组(26){[“url”]=>字符串(61)“ https://www.google.co.uk/?gws_rd=cr&ei=fIaOUsflOqnG0QXy74DYDg ”[“content_type”]=>字符串(24)“文本/ 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) ["pretransfer_time"]=> float (0.082954) ["size_upload"]=> float(0) ["size_download"]=> float(119772) ["speed_download"]=> 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"]=> 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 "]=> 字符串(0) "" }

于 2013-11-21T22:18:40.617 回答
1

啊!谷歌正在通过 GEO-IP 或类似方式定位您,并将您重定向到您当地的谷歌镜像。

请参阅:https ://webapps.stackexchange.com/questions/46591/what-does-gws-rd-cr-in-the-url-indicate

因此,当他们重定向您时,302 代码是正确的。

尝试使用 URL:https ://www.google.com/ncr (ncr 代表无国家/地区重定向),看看您的进展如何。

于 2013-11-21T22:24:39.550 回答