Solved

curl - test connection

Posted on 2009-05-18
7
850 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:Pedro Chagas
  • 3
  • 3
7 Comments
 
LVL 8

Assisted Solution

by:DocSeltsam
DocSeltsam earned 125 total points
ID: 24411446
Hi there,

having a short look on the php site ( http://de2.php.net/manual/de/function.curl-init.php ),
the following might fit your needs...

It will return the retrieved bytes if everything goes well and FALSE if it doesn't

--TheDoctor

function webcheck ($url) {
        $ch = curl_init ($url) ;
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1) ;
        $res = curl_exec ($ch) ;
        curl_close ($ch) ;
        return ($res) ;
}

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 375 total points
ID: 24413386
Here is what I use, modified slightly to return FALSE if it fails.  Check the TIMEOUT to see if that is OK for your needs.

HTH, ~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 $html;
}

Open in new window

0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 24413954
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
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Author Comment

by:Pedro Chagas
ID: 24414908
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
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 24415238
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

0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 24416321
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
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 24416347
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

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question