0

I have 2 codes written with PHP

<?php
error_reporting(1);

function url_exists($url) 
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_NOBODY, TRUE);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $status = array();
    preg_match('/HTTP\/.* ([0-9]+) .*/', curl_exec($ch) , $status);
    return ($status[1] == 200);
}

echo "EX:".url_exists("http://www.google.com");

?>

and

<?php
error_reporting(1);

$ch = curl_init("www.yahoo.com");  
curl_setopt($ch, CURLOPT_TIMEOUT, 2);  
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
$data = curl_exec($ch);  
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
curl_close($ch);  
if($httpcode>=200 && $httpcode<300)
{  
    echo "Found@";  
} 
else 
{  
    echo "Not found";  
}  

?>

None of this of above works? WHY??? Yesterday worked pretty good, why PHP cannot be explain some times?

I have curl extension on from php.ini ... i tried on 3 different servers ...

What i'm doing wrong? Thank you.

Master345
  • 2,250
  • 11
  • 38
  • 50
  • What does not work? Error message? – PiTheNumber Mar 07 '12 at 10:41
  • just nothning, just wont return 1 or 0 ... believe me, or returns as if google.com wont exists – Master345 Mar 07 '12 at 10:56
  • None of your scripts will return 1 or 0 in any case! Please google "js debug". Try `var_dump(curl_exec($ch));`. Also see my edit below about echo boolean values. – PiTheNumber Mar 07 '12 at 11:00
  • var_dump(curl_exec($ch)) of second code returns bool(false) ... god, this is so frustrating, worked good yesterday, and i never touch the computer, i have deep freeze installed ... – Master345 Mar 07 '12 at 11:08

1 Answers1

1
echo "EX:".url_exists("http://www.google.com");

will always echo EX: because url_exists returns a boolean. Try:

echo "EX:" . (url_exists("http://www.google.com") ? 'true' : 'false');

Maybe this works?

$ch = curl_init("http://www.example.com/favicon.ico");
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// $retcode > 400 -> not found, $retcode = 200, found.
curl_close($ch);

Source https://stackoverflow.com/a/982045/956397

Community
  • 1
  • 1
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
  • what about the second code? it returns "not found" every time, no matter what link i put there – Master345 Mar 07 '12 at 11:11
  • That's what I mean with debugging! `echo $httpcode;` => '302', google httpcode 302 => redirect, google curl follow redirect => http://stackoverflow.com/questions/3519939/make-curl-follow-redirects – PiTheNumber Mar 07 '12 at 11:15
  • yes you are right, it was returning 302, thanks, and i put somewhare trim() that solves the problem ... oh ... php can be sometimes hard, and about that follow redirect, you put one line, witch i don't need, but thanks – Master345 Mar 07 '12 at 12:25