Here is a simple cURL test. Obviously this domain does not exist yet when I run this script it returns True - huh? I'm just trying to determine if the URL connects or not. $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://sfhsfhshfjsdhf.com"); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 0); $status = curl_exec($ch); curl_close($ch); if ($status) { print "true"; } else { print "false"; } PHP:
hi, You choose wrong way to check curl_exec result. Try this: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://asdasd.com"); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 0); $status = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if( $code ) { echo "COONECTION ERROR OR NOT HTTP PROTOCOL"; } else { echo "CONNECTION OK, ANSWER CODE: $code"; } curl_close($ch); PHP:
Thanks but that always returns "CONNECTION ERROR OR NOT HTTP PROTOCOL" regardless of what URL is used ???
I'm only trying to check to see if the domain connects at all -- I don't care if its a 404 as long as it connects. I'd like to be able to detect if it can connect or not connect.
Something must be wrong with my PC and/or my internet connection as I always get the same results regardless of what URL I use. I have a new HP PC and cable internet connection. I've cleared my cache and tried two browsers: IE7 and FF. I'm doomed!
curl will always get a result even when theres no connections. When a domain is not hosted, it will return the standard domain or page not found by your browser which will be pass into curl as a result. So in you getting a result regardless.
This is the way I found to get this to work the way I wanted: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://mywebsite.com"); curl_setopt($ch, CURLOPT_POST, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 0); $view = curl_exec($ch); if (eregi("a string from my site home page", $view)) { echo "ok"; } else { echo "fail"; } curl_close($ch); PHP:
Usually, i check whether the result is an empty string or not. If it is an empty string, i assume the domain can not be opened. Of course this is not always true What about set "CURLOPT_HEADER" into "1" ?