Solved

curl - test connection

Posted on 2009-05-18
7
833 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 108

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 108

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now