Solved

curl - test connection

Posted on 2009-05-18
7
828 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wordpress Query 1 35
PHP code to retrieve NDFD weather forecasts. 6 37
Creating a slider 12 34
Adding through query php 9 11
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
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…

760 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