使用 php ping 网站

发布于 2024-07-29 04:10:56 字数 1727 浏览 2 评论 0原文

我想创建一个 php 脚本,它将 ping 一个域并列出响应时间以及请求的总大小。

这将用于监控网站网络。 我用 curl 尝试过,这是我到目前为止的代码:

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;

}

这是使用 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;

}

有没有更好的方法来使用 php ping 网站?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

尸血腥色 2024-08-05 04:10:56

显然,curl 有各种很酷的东西,但请记住,您始终可以通过从命令行调用内置工具来使用它们,如下所示:

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

唯一要记住的一点是,这不会像curl那样跨平台可能; 尽管默认情况下也不启用curl扩展..

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

蓬勃野心 2024-08-05 04:10:56

当为一次性任务执行快速脚本时,我只需 exec() wget:

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

它很简单并且可以处理重定向。

如果您使用suhosin 补丁和curl,您可能会遇到http 重定向问题(301、302...),
苏霍辛不会允许的。

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.

木格 2024-08-05 04:10:56

我不确定 Curl/Fopen,但 这个 基准测试表明 file_get_contents 的性能比 fopen 更好。

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

末蓝 2024-08-05 04:10:56

您可以使用 xmlrpc (xmlrpc_client)。 不确定卷曲的优点/缺点是什么。

Drupal 使用 xmlrpc 来实现此目的(查看 ping 模块)。

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

倒带 2024-08-05 04:10:56

使用curl就可以了。

但不确定我是否会使用该用户代理字符串。 除非您特别需要,否则最好定制一个。

Using curl is fine.

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

好久不见√ 2024-08-05 04:10:56

也许这个梨 Net_Ping 就是你寻找。 它不再被维护,但它可以工作。

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

背叛残局 2024-08-05 04:10:56

如果启用了远程 fopen,file_get_contents() 也能实现这一目的。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文