Link to home
Start Free TrialLog in
Avatar of Pedro Chagas
Pedro ChagasFlag for Portugal

asked on

curl - test connection

hi e's, i have a script where i collect 10 url's.
i need a script in curl for check if that url's have connection.
i read some posts, and i thing curl is the best for do this.
i want to check all url's in the same script, like this:
call database
while(for get url)
curl// this is the part i need help.

regards, jc
SOLUTION
Avatar of DocSeltsam
DocSeltsam
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Pedro Chagas

ASKER

Hi @ray, thanks for the great script.
In the end of your script I put this lines:
////////////////////////////////////////////////////
$vision = my_curl("http://www.google.pt/search?q=free+photos&hl=pt-PT&start=10&sa=N");
echo $vision;
/////////////////////////////////////////////////////
I execute the script and in browser appear entire site http://www.google.pt/search?q=free+photos&hl=pt-PT&start=10&sa=N
For this example I don't want the site, but just if each site have connection (try for one or two seconds).

I like your script but I need to adapt to my necessity, so I need something like this:
If ($html == true){
 Connect
} else {
No connect
}

How I do that?

Regards, JC
Hi again @ray, I put in this way for check the connection:
if(my_curl("http://www.google.pt/search?q=free+photos&hl=pt-PT&start=10&sa=N")) {
 echo "Yes!";
} else {
 echo "No.";
}

Do you thing this is the best way?

What this line do "curl_setopt($curl, CURLOPT_TIMEOUT, 10);"?

Regards, JC
To just return True or False, try this.

The cURL options are documented here:
http://us.php.net/manual/en/function.curl-setopt.php

The CURLOPT_TIMEOUT is the maximum number of seconds to allow cURL functions to execute.

Best regards, ~Ray
function my_curl($url) {
// HEADERS FROM FIREFOX - APPEARS TO BE A BROWSER REFERRED BY GOOGLE
        $curl = curl_init();
 
        $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
        $header[] = "Cache-Control: max-age=0";
        $header[] = "Connection: keep-alive";
        $header[] = "Keep-Alive: 300";
        $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
        $header[] = "Accept-Language: en-us,en;q=0.5";
        $header[] = "Pragma: "; // browsers keep this blank.
 
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15');
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com');
        curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
        curl_setopt($curl, CURLOPT_AUTOREFERER, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
 
        if (!$html = curl_exec($curl)) { $html = FALSE; }
        curl_close($curl);
return TRUE;
}

Open in new window

Hi @ray, just one thing:
What is correct:
if (!$html = curl_exec($curl)) { $html = FALSE; } OR
if (!$html == curl_exec($curl)) { $html = FALSE; }

Regards, JC
The single equal sign is correct but I see an error in my example.  Corrected copy posted below.

This single-equal construct says, "run curl_exec and assign the output to $html, then test $html and if it is FALSE, it satisfies the 'if' statement."
function my_curl($url) {
// HEADERS FROM FIREFOX - APPEARS TO BE A BROWSER REFERRED BY GOOGLE
        $curl = curl_init();
 
        $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
        $header[] = "Cache-Control: max-age=0";
        $header[] = "Connection: keep-alive";
        $header[] = "Keep-Alive: 300";
        $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
        $header[] = "Accept-Language: en-us,en;q=0.5";
        $header[] = "Pragma: "; // browsers keep this blank.
 
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15');
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_REFERER, 'http://www.google.com');
        curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
        curl_setopt($curl, CURLOPT_AUTOREFERER, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
 
        if (!$html = curl_exec($curl)) 
        { 
return FALSE; 
        }
        curl_close($curl);
return TRUE;
}

Open in new window