Chekc URL status in cURL

Discussion in 'PHP' started by redhits, Sep 18, 2007.

  1. #1
    Does anybody known how i can check a website status (if offline/offline) with CURL?!

    something if*curl_exec* will only tell me that the curl was executed not that website return a 200 status code, or if the domain name doesn't exists, or server return me a 404 status code
     
    redhits, Sep 18, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    function fetch_url_status($url, $followlocation = true)
    {
    	$ch = curl_init($url);
    	
    	curl_setopt($ch, CURLOPT_NOBODY, true);
    	curl_setopt($ch, CURLOPT_HEADER, true);
    	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $followlocation );
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    	curl_exec($ch);
    	$info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    	curl_close($ch);
    	
    	return $info;
    }
    
    
    PHP:
     
    nico_swd, Sep 18, 2007 IP
  3. guruhowto

    guruhowto Guest

    Messages:
    75
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    from the function above it will give you an array of information.. from the above the array is $info

    To get the URL status or request code you can use $info["http_code"]

    200 for ok, 301 and 302 for redirected and etc...

    more info php.net/manual/en/function.curl-getinfo.php
     
    guruhowto, Sep 18, 2007 IP
  4. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #4

    Just one problem ... for an invalid domain name like

    softgroups22.com it will return status code 200

    only for softgroups.com/invalidpage.html will return 404 code...
     
    redhits, Sep 18, 2007 IP
  5. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Umm no? It will return 0 for invalid domain names.

    
    echo fetch_url_status('http://www.softgroups22.com/');
    
    PHP:
    Gives me 0.
     
    nico_swd, Sep 18, 2007 IP
  7. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    That's strange. Try echoing the HTML.

    
    $ch = curl_init('http://www.softgroups22.com/');
    
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);
    curl_close($ch);
    
    PHP:
    Just to see what you get...
     
    nico_swd, Sep 18, 2007 IP
  9. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #9
    If I am eacho the html i got a page with Server not found, 404...
     
    redhits, Sep 18, 2007 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    Err... that's an error on your server then. This is not a normal cURL behavior. Seems more like some kind of browser is being used or something...
     
    nico_swd, Sep 18, 2007 IP
  11. redhits

    redhits Notable Member

    Messages:
    3,023
    Likes Received:
    277
    Best Answers:
    0
    Trophy Points:
    255
    #11
    How curl will behave when you try to output html code from an non existing domain name? won;t it output a page with "Server not found",etc?!
     
    redhits, Sep 18, 2007 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    No, this message is generated by your browser. You'll get a different looking message on each browser. cURL won't return any output at all if there's no server found.
     
    nico_swd, Sep 18, 2007 IP