簡體   English   中英

用php phar ping

[英]ping with php phar

我想使用此腳本來執行ping而不使用exec(); 或與此類似的命令。

問題是我得到這些錯誤:

嚴格標准:不應在第3行的C:\\ xampp \\ htdocs \\ test.php中靜態調用非靜態方法Net_Ping :: factory()

嚴格標准:非靜態方法Net_Ping :: _ setSystemName()不應在141行的C:\\ xampp \\ php \\ PEAR \\ Net \\ Ping.php中靜態調用

嚴格標准:不應在第143行的C:\\ xampp \\ php \\ PEAR \\ Net \\ Ping.php中靜態調用Net_Ping :: _ setPingPath()非靜態方法

嚴格標准:不應在第4行的C:\\ xampp \\ htdocs \\ test.php中靜態調用非靜態方法PEAR :: isError()

test.php上的代碼

<?php
require_once "Net/Ping.php";
$ping = Net_Ping::factory();
if (PEAR::isError($ping)) {
    echo $ping->getMessage();
} else {
    $ping->setArgs(array('count' => 2));
    var_dump($ping->ping('example.com'));
}
?>

沒錯,PEAR組件不適合E_STRICT。 您擁有的代碼還可以,但是PEAR代碼沒有說該方法是靜態的,因此PHP將發出E_STRICT警告。 這不是您真正可以更改的東西,但是您可以通過調整error_reporting設置選擇忽略它。

<?php
// before PEAR stuff.
$errLevel = error_reporting( E_ALL );

// PEAR stuff.
require_once "Net/Ping.php";
$ping = Net_Ping::factory();
if (PEAR::isError($ping)) {
    echo $ping->getMessage();
} else {
    $ping->setArgs(array('count' => 2));
    $result = $ping->ping('example.com');
}

// restore the original error level.
error_reporting( $errLevel );
var_dump( $result );

是我去年在不需要PEAR的系統上進行此操作時編寫的ping類。

用法示例:

$ping = new ping();
if (!$ping->setHost('google.com')) exit('Could not set host: '.$this->getLastErrorStr());
var_dump($ping->send());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM