Solved

curl - test connection

Posted on 2009-05-18
7
840 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 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