Get the right http status code php

biox picture biox · Nov 21, 2013 · Viewed 13.3k times · Source

My code give me http status code 302 when I visit google but when I visiting with firefox browser and checking the status with firebug, there the status is 200.

Here is my code:

$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);

I tried also get_headers but it giving me 301 status

get_headers($url);

EDIT: the result is below

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)

Answer

steve picture steve · Nov 21, 2013

Which URL are you using for Google?

I used your code above, setting:

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

and received a 200 response in the curl info.

The full code I'm testing with:

Strange - I'm using:

$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);

The var_dump output I receive back:

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) ["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"]=> string(0) "" }