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

最初の反復の後に返されるため、結果は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 に答える