tags:

views:

763

answers:

7

I want to create a php script that will ping a domain and list the response time along with the total size of the request. This will be used for monitoring a network of websites. I took a pass at it with curl. Can you think of a better option?

This is mostly all from comments on php.net and more of a proof of concept then anything else. I know it's not the cleanest code.

function curlTest2($url) {

 clearstatcache();

 $return = '';

 if(substr($url,0,4)!="http") $url = "http://".$url;

 $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';

 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_HEADER, 1);
 curl_setopt($ch, CURLOPT_NOBODY, 1);
 curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 15);
 curl_setopt($ch, CURLOPT_FAILONERROR, 1);
 curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);

 $execute = curl_exec($ch);

 // Check if any error occured
 if(!curl_errno($ch)) {
  $bytes   = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
  $total_time = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
  $return = 'Took ' . $total_time . ' / Bytes: '. $bytes;        
 } else {
  $return = 'Error reaching domain';
 }
 curl_close($ch);

 return $return;

}

And here is one using fopen

function fopenTest($link) {

 if(substr($link,0,4)!="http"){ 
 $link = "http://".$link;
 }

 $timestart = microtime();

 $churl = @fopen($link,'r');

 $timeend = microtime();
 $diff = number_format(((substr($timeend,0,9)) + (substr($timeend,-10)) - (substr($timestart,0,9)) - (substr($timestart,-10))),4);
 $diff = $diff*100;

 if (!$churl) {
  $message="Offline";
 }else{
  $message="Online. Time : ".$diff."ms ";
 }

 fclose($churl); 

 return  $message;

}
A: 

You could use xmlrpc (xmlrpc_client). Not sure what the advantages/disadvantages to curl are.

Drupal uses xmlrpc for this purpose (look at the ping module).

txwikinger
Are you talking about drupal.org/handbook/modules/ping? That doesn't look to be what I am looking for...
Louis W
I also think it'd be fine to use this. At a pedantic guess, maybe this option would require extra xml parsing internally compared to the straightforward curl option? dunno really.
David Archer
A: 

Using curl is fine.

Not sure if I'd use that useragent string though. Rather make a custom one unless you specifically need to.

David Archer
A: 

maybe this pear Net_Ping is what you are looking for. It's no more maintained but it works.

dam
+1  A: 

When doing quick scripts for one time tasks I just exec() wget:

$response = `wget http://google.com -O -`;

It's simple and takes care of redirects.

If you're using suhosin patches and curl you may encounter problems with http redirect (301, 302...), suhosin won't allow it.

niteria
+1  A: 

I'm not sure about Curl/Fopen but this benchmark says file_get_contents have better performance then fopen.

Ahmet Kakıcı
+4  A: 

Obviously curl's got all kinds of cool things, but remember, you can always make use of built in tools by invoking them from the command line like this:

$site = "google.com";
ob_start();
system("ping " . escapeshellarg($site));
print ob_end_flush();

Only thing to keep in mind, this isn't going to be as cross platform as curl might be; although the curl extension is not enabled by default either..

Stephen J. Fuhry
I'd do it this way - that way, if you did see a problem, you could be confident that the results you're seeing are valid.
EvilChookie
Ever heard of the escapeshellarg() function? http://ca.php.net/manual/en/function.escapeshellarg.php
Andrew Moore
The problem with this is that it will ping the server to see if it's alive rather than find out if the webserver is alive as cURL will...Also cURL would be site specific rather than server specific (useful in testing a virtual host environment.
Shadi Almosri
escapeshellarg = brilliant, thanks.also, good call on cURL testing the webserver and not the server, I hadn't thought of that.
Stephen J. Fuhry
A: 

If remote fopen is enabled, file_get_contents() will do the trick too.

ceejayoz