Debt Consolidation - Reno Nevada Real Estate - Calendar news - Debt Consolidation - Myspace Layouts

PDA

View Full Version : Chekc URL status in cURL


softgroups
Sep 18th 2007, 12:16 pm
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

nico_swd
Sep 18th 2007, 12:24 pm
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;
}

guruhowto
Sep 18th 2007, 12:30 pm
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

softgroups
Sep 18th 2007, 12:33 pm
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;
}




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...

softgroups
Sep 18th 2007, 12:34 pm
Array ( [url] => http://softroups2222.com [content_type] => text/html [http_code] => 200 [header_size] => 357 [request_size] => 120 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.004634 [namelookup_time] => 0.003127 [connect_time] => 0.003398 [pretransfer_time] => 0.003442 [size_upload] => 0 [size_download] => 2860 [speed_download] => 617177 [speed_upload] => 0 [download_content_length] => 2860 [upload_content_length] => 0 [starttransfer_time] => 0.004407 [redirect_time] => 0 )

nico_swd
Sep 18th 2007, 12:50 pm
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...

Umm no? It will return 0 for invalid domain names.


echo fetch_url_status('http://www.softgroups22.com/');


Gives me 0.

softgroups
Sep 18th 2007, 1:02 pm
I get 200
http://free.realranker.com/21.php?url=softroups21.com
always

nico_swd
Sep 18th 2007, 2:19 pm
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);


Just to see what you get...

softgroups
Sep 18th 2007, 4:01 pm
If I am eacho the html i got a page with Server not found, 404...

nico_swd
Sep 18th 2007, 4:18 pm
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...

softgroups
Sep 18th 2007, 4:32 pm
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?!

nico_swd
Sep 18th 2007, 4:37 pm
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.