1

这应该为每个适配器(输出成对)返回 Rx 和 Tx 的原始值,只有适配器传输了多于零的数据。

<?php
//windows network usage testing
function win_netinfo(){
    ob_start();
    $wmi = new COM("Winmgmts://");
    $nets = $wmi->execquery("SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface WHERE BytesSentPersec > 1");
        foreach ($nets as $net)
        {
            $net_txbytes = $net->BytesSentPersec;
            $net_rxbytes = $net->BytesReceivedPersec;
            if ($net_txbytes < 0) {
            $net_txbytes = $net->BytesTotalPersec - $net->BytesReceivedPersec;

            }
            if ($net_rxbytes < 0) {
            $net_rxbytes = $net->BytesTotalPersec - $net->BytesSentPersec;
            }
            return $net_txbytes . "</br>"; //When it RETURNS it only shows the FIRST value... :/ WHAT DO?
            }
}
echo win_netinfo();

?>

输出应该是这样的:

21936313136
12345163517
13647613
87653467
546254
246247
87653642
24583462

(对于每个适配器,每隔一行是 Rx 或 Tx)。

当前有效的代码:

<?php
    # Peport All Errors
    error_reporting(E_ALL);

    # Create Object
    $wmi = new COM("Winmgmts://");

    # Get net info
    $oss = $wmi->execquery("SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface");

    # Show net info
    foreach($oss as $os)
    {
        echo "BytesReceivedPerSec: " . $os->BytesReceivedPerSec . "<br />";
        echo "BytesSentPerSec: " . $os->BytesSentPerSec . "<br />";
        echo "BytesTotalPerSec: " . $os->BytesTotalPerSec . "<br />";
        echo "Caption: " . $os->Caption . "<br />";
        echo "CurrentBandwidth: " . $os->CurrentBandwidth . "<br />";
        echo "Description: " . $os->Description . "<br />";    
        echo "Frequency_Object: " . $os->Frequency_Object . " kb<br />";
        echo "Frequency_PerfTime: " . $os->Frequency_PerfTime . "<br />";
        echo "Frequency_Sys100NS: " . $os->Frequency_Sys100NS . "<br />";
        echo "Name: " . $os->Name . "<br />";
        echo "OutputQueueLength: " . $os->OutputQueueLength . "<br />";
        echo "PacketsOutboundDiscarded: " . $os->PacketsOutboundDiscarded . "<br />";
        echo "PacketsOutboundErrors: " . $os->PacketsOutboundErrors . "<br />";
        echo "PacketsReceivedNonUnicastPerSec: " . $os->PacketsReceivedNonUnicastPerSec . "<br />";
        echo "PacketsReceivedPerSec: " . $os->PacketsReceivedPerSec . "<br />";
        echo "PacketsReceivedUnicastPerSec: " . $os->PacketsReceivedUnicastPerSec . "<br />";
        echo "PacketsReceivedUnknown: " . $os->PacketsReceivedUnknown . "<br />";
        echo "PacketsSentNonUnicastPerSec: " . $os->PacketsSentNonUnicastPerSec . "<br />";
        echo "PacketsSentPerSec: " . $os->PacketsSentPerSec . "<br />";
        echo "PacketsSentUnicastPerSec: " . $os->PacketsSentUnicastPerSec . "<br />";
        echo "Timestamp_Object: " . $os->Timestamp_Object . "<br />";
        echo "Timestamp_PerfTime: " . $os->Timestamp_PerfTime . "<br />";
        echo "Timestamp_Sys100NS: " . $os->Timestamp_Sys100NS . "<br />";   
        echo "<br /><br /><br />";     
    }

?>
4

1 回答 1

1

您在第一次迭代后返回,因此只有一个结果,下面是我将如何通过创建一个数组并返回它来做到这一点:

<?php
//windows network usage testing
function win_netinfo(){
    $wmi = new COM("Winmgmts://");
    $nets = $wmi->execquery("SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface WHERE BytesSentPersec > 1");
    $return=array();
    $i=0;
    foreach ($nets as $net){

        $return[$i]['name']=$net->Name;

        if ($net->BytesSentPersec < 0) {
            $return[$i]['net_txbytes'] = $net->BytesTotalPersec - $net->BytesReceivedPersec;
        }else{
            $return[$i]['net_txbytes'] = $net->BytesSentPersec;
        }

        if ($net->BytesReceivedPersec < 0) {
            $return[$i]['net_rxbytes'] = $net->BytesTotalPersec - $net->BytesSentPersec;
        }else{
            $return[$i]['net_rxbytes'] = $net->BytesReceivedPersec;
        }
        $i++;
    }
    return $return;
}

$adapters=win_netinfo();
/*Array
Array
(
    [0] => Array
        (
            [name] => VirtualBox Host-Only Ethernet Adapter
            [net_txbytes] => 5095956
            [net_rxbytes] => 0
        )

    [1] => Array
        (
            [name] => Belkin 54g Wireless USB Network Adapter
            [net_txbytes] => 532337967
            [net_rxbytes] => 5252455518
        )

)
*/
?>

并输出数组,如下所示:

<?php
foreach($adapters as $adapter){
    echo '<p>'.$adapter['name'].' - Sent: '.$adapter['net_txbytes'].' bytes - Received: '.$adapter['net_rxbytes'].' bytes</p>';
}
?>
于 2012-04-07T18:27:55.737 回答