Using php to ping a website

戏子无情 提交于 2019-12-10 20:09:02

问题


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 tried it with curl, here is the code I have so far:

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;

}

Is there a better way to ping a website using php?


回答1:


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).




回答2:


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..




回答3:


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.




回答4:


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




回答5:


Using curl is fine.

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




回答6:


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




回答7:


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



来源:https://stackoverflow.com/questions/1155512/using-php-to-ping-a-website

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!