2016-07-01 29 views
1

我需要从xml导入一个值,我尝试了很多类型,但没有成功。PHP返回XML值phpsysinfo

这是我的XML:

<tns:phpsysinfo xsi:schemaLocation="http://phpsysinfo.sourceforge.net/ phpsysinfo3.xsd"><Generation version="3.2.5" timestamp="1467404801"/><Options tempFormat="c" byteFormat="auto_binary" refresh="60000" threshold="90"/><Vitals Hostname="raspberrypi" IPAddr="192.168.1.200" Kernel="4.1.19-v7+ (SMP) armv7l" Distro="Raspbian GNU/Linux 7 (wheezy)" Distroicon="Raspbian.png" Uptime="83558.17" Users="0" LoadAvg="0.03 0.04 0.05" SysLang="Portuguese Brazil (pt_BR)" CodePage="ISO-8859-1" Processes="124" ProcessesRunning="3" ProcessesSleeping="121" OS="Linux"/><Network><NetDevice Name="lo" RxBytes="433813" TxBytes="433813" Err="0" Drops="0" Info="127.0.0.1"/><NetDevice Name="eth0" RxBytes="13260816" TxBytes="54273067" Err="0" Drops="10" Info="B8-27-EB-96-CE-29;192.168.1.200;100Mb/s full"/></Network><Hardware><USB><Device Name="Standard Microsystems Corp." Count="2"/><Device Name="Linux Foundation 2.0 root hub" Count="1"/></USB><CPU><CpuCore Model="ARMv7 Processor rev 5 (v7l)" CpuSpeed="900" CpuSpeedMax="900" CpuSpeedMin="600" Bogomips="57.60"/><CpuCore Model="ARMv7 Processor rev 5 (v7l)" CpuSpeed="900" CpuSpeedMax="900" CpuSpeedMin="600" Bogomips="57.60"/><CpuCore Model="ARMv7 Processor rev 5 (v7l)" CpuSpeed="900" CpuSpeedMax="900" CpuSpeedMin="600" Bogomips="57.60"/><CpuCore Model="ARMv7 Processor rev 5 (v7l)" CpuSpeed="900" CpuSpeedMax="900" CpuSpeedMin="600" Bogomips="57.60"/></CPU></Hardware><Memory Free="433508352" Used="537260032" Total="970768384" Percent="55"><Details App="209883136" AppPercent="22" Buffers="85745664" BuffersPercent="8" Cached="241631232" CachedPercent="25"/><Swap Free="104853504" Used="0" Total="104853504" Percent="0"><Mount MountPointID="1" Name="SWAP" Free="104853504" Used="0" Total="104853504" Percent="0" MountPoint="/var/swap"/></Swap></Memory><FileSystem><Mount MountPointID="1" FSType="ext4" Name="/dev/root" Free="2596655104" Used="4733452288" Total="7694135296" Percent="62" MountOptions="rw, noatime, data=ordered" Inodes="23" MountPoint="/"/><Mount MountPointID="2" FSType="devtmpfs" Name="devtmpfs" Free="480960512" Used="0" Total="480960512" Percent="0" MountOptions="rw, relatime, size=469688k, nr_inodes=117422, mode=755" Inodes="1" MountPoint="/dev"/><Mount MountPointID="3" FSType="tmpfs" Name="tmpfs" Free="96829440" Used="249856" Total="97079296" Percent="0" MountOptions="rw, nosuid, noexec, relatime, size=94804k, mode=755" Inodes="1" MountPoint="/run"/><Mount MountPointID="4" FSType="tmpfs" Name="tmpfs" Free="5242880" Used="0" Total="5242880" Percent="0" MountOptions="rw, nosuid, nodev, noexec, relatime, size=5120k" Inodes="1" MountPoint="/run/lock"/><Mount MountPointID="5" FSType="tmpfs" Name="tmpfs" Free="194150400" Used="0" Total="194150400" Percent="0" MountOptions="rw, nosuid, nodev, noexec, relatime, size=189600k" Inodes="1" MountPoint="/run/shm"/><Mount MountPointID="6" FSType="vfat" Name="/dev/mmcblk0p1" Free="37789696" Used="20873216" Total="58662912" Percent="36" MountOptions="rw, relatime, fmask=0022, dmask=0022, codepage=437, iocharset=ascii, shortname=mixed, errors=remount-ro" MountPoint="/boot"/></FileSystem><MBInfo/><UPSInfo/><Plugins/><Errors/></tns:phpsysinfo> 

我需要从内存(百分比= “55”)得到的百分比值,但我不能。

这是我的PHP代码:

<?php 
$api = "http://127.0.0.1/sysinfo/xml.php?plugin=complete"; 
$resultado = file_get_contents($api); 
$xml = simplexml_load_string($resultado); 
foreach ($xml as $id): 
$result = $id->Memor->Free; 
var_dump($id); 
endforeach; 
?> 

这回吧:

object(SimpleXMLElement)#3 (1) { ["@attributes"]=> array(2) { ["version"]=> string(5) "3.2.5" ["timestamp"]=> string(10) "1467406132" } } object(SimpleXMLElement)#4 (1) { ["@attributes"]=> array(4) { ["tempFormat"]=> string(1) "c" ["byteFormat"]=> string(11) "auto_binary" ["refresh"]=> string(5) "60000" ["threshold"]=> string(2) "90" } } object(SimpleXMLElement)#3 (1) { ["@attributes"]=> array(14) { ["Hostname"]=> string(11) "raspberrypi" ["IPAddr"]=> string(9) "127.0.0.1" ["Kernel"]=> string(23) "4.1.19-v7+ (SMP) armv7l" ["Distro"]=> string(29) "Raspbian GNU/Linux 7 (wheezy)" ["Distroicon"]=> string(12) "Raspbian.png" ["Uptime"]=> string(8) "84889.27" ["Users"]=> string(1) "0" ["LoadAvg"]=> string(14) "0.00 0.01 0.05" ["SysLang"]=> string(25) "Portuguese Brazil (pt_BR)" ["CodePage"]=> string(10) "ISO-8859-1" ["Processes"]=> string(3) "124" ["ProcessesRunning"]=> string(1) "3" ["ProcessesSleeping"]=> string(3) "121" ["OS"]=> string(5) "Linux" } } object(SimpleXMLElement)#4 (1) { ["NetDevice"]=> array(2) { [0]=> object(SimpleXMLElement)#3 (1) { ["@attributes"]=> array(6) { ["Name"]=> string(2) "lo" ["RxBytes"]=> string(6) "574843" ["TxBytes"]=> string(6) "574843" ["Err"]=> string(1) "0" ["Drops"]=> string(1) "0" ["Info"]=> string(9) "127.0.0.1" } } [1]=> object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(6) { ["Name"]=> string(4) "eth0" ["RxBytes"]=> string(8) "13364926" ["TxBytes"]=> string(8) "54398475" ["Err"]=> string(1) "0" ["Drops"]=> string(2) "10" ["Info"]=> string(44) "B8-27-EB-96-CE-29;192.168.1.200;100Mb/s full" } } } } object(SimpleXMLElement)#5 (2) { ["USB"]=> object(SimpleXMLElement)#4 (1) { ["Device"]=> array(2) { [0]=> object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(2) { ["Name"]=> string(27) "Standard Microsystems Corp." ["Count"]=> string(1) "2" } } [1]=> object(SimpleXMLElement)#7 (1) { ["@attributes"]=> array(2) { ["Name"]=> string(29) "Linux Foundation 2.0 root hub" ["Count"]=> string(1) "1" } } } } ["CPU"]=> object(SimpleXMLElement)#3 (1) { ["CpuCore"]=> array(4) { [0]=> object(SimpleXMLElement)#7 (1) { ["@attributes"]=> array(5) { ["Model"]=> string(27) "ARMv7 Processor rev 5 (v7l)" ["CpuSpeed"]=> string(3) "900" ["CpuSpeedMax"]=> string(3) "900" ["CpuSpeedMin"]=> string(3) "600" ["Bogomips"]=> string(5) "57.60" } } [1]=> object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(5) { ["Model"]=> string(27) "ARMv7 Processor rev 5 (v7l)" ["CpuSpeed"]=> string(3) "900" ["CpuSpeedMax"]=> string(3) "900" ["CpuSpeedMin"]=> string(3) "600" ["Bogomips"]=> string(5) "57.60" } } [2]=> object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(5) { ["Model"]=> string(27) "ARMv7 Processor rev 5 (v7l)" ["CpuSpeed"]=> string(3) "900" ["CpuSpeedMax"]=> string(3) "900" ["CpuSpeedMin"]=> string(3) "600" ["Bogomips"]=> string(5) "57.60" } } [3]=> object(SimpleXMLElement)#9 (1) { ["@attributes"]=> array(5) { ["Model"]=> string(27) "ARMv7 Processor rev 5 (v7l)" ["CpuSpeed"]=> string(3) "900" ["CpuSpeedMax"]=> string(3) "900" ["CpuSpeedMin"]=> string(3) "600" ["Bogomips"]=> string(5) "57.60" } } } } } object(SimpleXMLElement)#3 (3) { ["@attributes"]=> array(4) { ["Free"]=> string(9) "432721920" ["Used"]=> string(9) "538046464" ["Total"]=> string(9) "970768384" ["Percent"]=> string(2) "55" } ["Details"]=> object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(6) { ["App"]=> string(9) "210321408" ["AppPercent"]=> string(2) "22" ["Buffers"]=> string(8) "86069248" ["BuffersPercent"]=> string(1) "8" ["Cached"]=> string(9) "241655808" ["CachedPercent"]=> string(2) "25" } } ["Swap"]=> object(SimpleXMLElement)#4 (2) { ["@attributes"]=> array(4) { ["Free"]=> string(9) "104853504" ["Used"]=> string(1) "0" ["Total"]=> string(9) "104853504" ["Percent"]=> string(1) "0" } ["Mount"]=> object(SimpleXMLElement)#9 (1) { ["@attributes"]=> array(7) { ["MountPointID"]=> string(1) "1" ["Name"]=> string(4) "SWAP" ["Free"]=> string(9) "104853504" ["Used"]=> string(1) "0" ["Total"]=> string(9) "104853504" ["Percent"]=> string(1) "0" ["MountPoint"]=> string(9) "/var/swap" } } } } object(SimpleXMLElement)#4 (1) { ["Mount"]=> array(6) { [0]=> object(SimpleXMLElement)#3 (1) { ["@attributes"]=> array(10) { ["MountPointID"]=> string(1) "1" ["FSType"]=> string(4) "ext4" ["Name"]=> string(9) "/dev/root" ["Free"]=> string(10) "2596630528" ["Used"]=> string(10) "4733476864" ["Total"]=> string(10) "7694135296" ["Percent"]=> string(2) "62" ["MountOptions"]=> string(25) "rw, noatime, data=ordered" ["Inodes"]=> string(2) "23" ["MountPoint"]=> string(1) "/" } } [1]=> object(SimpleXMLElement)#5 (1) { ["@attributes"]=> array(10) { ["MountPointID"]=> string(1) "2" ["FSType"]=> string(8) "devtmpfs" ["Name"]=> string(8) "devtmpfs" ["Free"]=> string(9) "480960512" ["Used"]=> string(1) "0" ["Total"]=> string(9) "480960512" ["Percent"]=> string(1) "0" ["MountOptions"]=> string(54) "rw, relatime, size=469688k, nr_inodes=117422, mode=755" ["Inodes"]=> string(1) "1" ["MountPoint"]=> string(4) "/dev" } } [2]=> object(SimpleXMLElement)#9 (1) { ["@attributes"]=> array(10) { ["MountPointID"]=> string(1) "3" ["FSType"]=> string(5) "tmpfs" ["Name"]=> string(5) "tmpfs" ["Free"]=> string(8) "96829440" ["Used"]=> string(6) "249856" ["Total"]=> string(8) "97079296" ["Percent"]=> string(1) "0" ["MountOptions"]=> string(51) "rw, nosuid, noexec, relatime, size=94804k, mode=755" ["Inodes"]=> string(1) "1" ["MountPoint"]=> string(4) "/run" } } [3]=> object(SimpleXMLElement)#8 (1) { ["@attributes"]=> array(10) { ["MountPointID"]=> string(1) "4" ["FSType"]=> string(5) "tmpfs" ["Name"]=> string(5) "tmpfs" ["Free"]=> string(7) "5242880" ["Used"]=> string(1) "0" ["Total"]=> string(7) "5242880" ["Percent"]=> string(1) "0" ["MountOptions"]=> string(47) "rw, nosuid, nodev, noexec, relatime, size=5120k" ["Inodes"]=> string(1) "1" ["MountPoint"]=> string(9) "/run/lock" } } [4]=> object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(10) { ["MountPointID"]=> string(1) "5" ["FSType"]=> string(5) "tmpfs" ["Name"]=> string(5) "tmpfs" ["Free"]=> string(9) "194150400" ["Used"]=> string(1) "0" ["Total"]=> string(9) "194150400" ["Percent"]=> string(1) "0" ["MountOptions"]=> string(49) "rw, nosuid, nodev, noexec, relatime, size=189600k" ["Inodes"]=> string(1) "1" ["MountPoint"]=> string(8) "/run/shm" } } [5]=> object(SimpleXMLElement)#7 (1) { ["@attributes"]=> array(9) { ["MountPointID"]=> string(1) "6" ["FSType"]=> string(4) "vfat" ["Name"]=> string(14) "/dev/mmcblk0p1" ["Free"]=> string(8) "37789696" ["Used"]=> string(8) "20873216" ["Total"]=> string(8) "58662912" ["Percent"]=> string(2) "36" ["MountOptions"]=> string(103) "rw, relatime, fmask=0022, dmask=0022, codepage=437, iocharset=ascii, shortname=mixed, errors=remount-ro" ["MountPoint"]=> string(5) "/boot" } } } } object(SimpleXMLElement)#7 (0) { } object(SimpleXMLElement)#4 (0) { } object(SimpleXMLElement)#7 (0) { } object(SimpleXMLElement)#4 (0) { } 

有人能帮助我吗?

非常感谢。

+1

请解释'$ id-> Memor-> Free;'。你的XML中的“Memor”在哪里?我没看到它。 – PaulH

+0

对不起,正确的是 $ id-> Memory-> Free; –

+0

'foreach'隐藏内存,改为使用'$ xml->内存' – PaulH

回答

0

您几乎在那里:-)
访问SimpleXML内容并不直观。
我看到你的XML结构为:

<tns> 
    <Memory Free="433508352" Percent="55"></Memory> 
</tns> 

由于百分比是一个属性,它不能与->Percent访问。
$xml->Memory['Percent']是获取<Memory />Percent属性值的简单方法。于是放下foreach循环和使用:

$xml = simplexml_load_string($resultado); 
print_r((string) $xml->Memory['Percent']); 

看到http://phpfiddle.org/main/code/cgzf-zj4s

运行的代码下面是完整的XML,格式化。这对了解你在做什么很有用:

<tns:phpsysinfo xsi:schemaLocation="http://phpsysinfo.sourceforge.net/ phpsysinfo3.xsd"> 
    <Generation version="3.2.5" timestamp="1467404801"/> 
    <Options tempFormat="c" byteFormat="auto_binary" refresh="60000" threshold="90"/> 
    <Vitals Hostname="raspberrypi" IPAddr="192.168.1.200" Kernel="4.1.19-v7+ (SMP) armv7l" Distro="Raspbian GNU/Linux 7 (wheezy)" Distroicon="Raspbian.png" Uptime="83558.17" Users="0" LoadAvg="0.03 0.04 0.05" SysLang="Portuguese Brazil (pt_BR)" CodePage="ISO-8859-1" Processes="124" ProcessesRunning="3" ProcessesSleeping="121" OS="Linux"/> 
    <Network> 
     <NetDevice Name="lo" RxBytes="433813" TxBytes="433813" Err="0" Drops="0" Info="127.0.0.1"/> 
     <NetDevice Name="eth0" RxBytes="13260816" TxBytes="54273067" Err="0" Drops="10" Info="B8-27-EB-96-CE-29;192.168.1.200;100Mb/s full"/> 
    </Network> 
    <Hardware> 
     <USB> 
      <Device Name="Standard Microsystems Corp." Count="2"/> 
      <Device Name="Linux Foundation 2.0 root hub" Count="1"/> 
     </USB> 
     <CPU> 
      <CpuCore Model="ARMv7 Processor rev 5 (v7l)" CpuSpeed="900" CpuSpeedMax="900" CpuSpeedMin="600" Bogomips="57.60"/> 
      <CpuCore Model="ARMv7 Processor rev 5 (v7l)" CpuSpeed="900" CpuSpeedMax="900" CpuSpeedMin="600" Bogomips="57.60"/> 
      <CpuCore Model="ARMv7 Processor rev 5 (v7l)" CpuSpeed="900" CpuSpeedMax="900" CpuSpeedMin="600" Bogomips="57.60"/> 
      <CpuCore Model="ARMv7 Processor rev 5 (v7l)" CpuSpeed="900" CpuSpeedMax="900" CpuSpeedMin="600" Bogomips="57.60"/> 
     </CPU> 
    </Hardware> 
    <Memory Free="433508352" Used="537260032" Total="970768384" Percent="55"> 
     <Details App="209883136" AppPercent="22" Buffers="85745664" BuffersPercent="8" Cached="241631232" CachedPercent="25"/> 
     <Swap Free="104853504" Used="0" Total="104853504" Percent="0"> 
      <Mount MountPointID="1" Name="SWAP" Free="104853504" Used="0" Total="104853504" Percent="0" MountPoint="/var/swap"/> 
     </Swap> 
    </Memory> 
    <FileSystem> 
     <Mount MountPointID="1" FSType="ext4" Name="/dev/root" Free="2596655104" Used="4733452288" Total="7694135296" Percent="62" MountOptions="rw, noatime, data=ordered" Inodes="23" MountPoint="/"/> 
     <Mount MountPointID="2" FSType="devtmpfs" Name="devtmpfs" Free="480960512" Used="0" Total="480960512" Percent="0" MountOptions="rw, relatime, size=469688k, nr_inodes=117422, mode=755" Inodes="1" MountPoint="/dev"/> 
     <Mount MountPointID="3" FSType="tmpfs" Name="tmpfs" Free="96829440" Used="249856" Total="97079296" Percent="0" MountOptions="rw, nosuid, noexec, relatime, size=94804k, mode=755" Inodes="1" MountPoint="/run"/> 
     <Mount MountPointID="4" FSType="tmpfs" Name="tmpfs" Free="5242880" Used="0" Total="5242880" Percent="0" MountOptions="rw, nosuid, nodev, noexec, relatime, size=5120k" Inodes="1" MountPoint="/run/lock"/> 
     <Mount MountPointID="5" FSType="tmpfs" Name="tmpfs" Free="194150400" Used="0" Total="194150400" Percent="0" MountOptions="rw, nosuid, nodev, noexec, relatime, size=189600k" Inodes="1" MountPoint="/run/shm"/> 
     <Mount MountPointID="6" FSType="vfat" Name="/dev/mmcblk0p1" Free="37789696" Used="20873216" Total="58662912" Percent="36" MountOptions="rw, relatime, fmask=0022, dmask=0022, codepage=437, iocharset=ascii, shortname=mixed, errors=remount-ro" MountPoint="/boot"/> 
    </FileSystem> 
    <MBInfo/> 
    <UPSInfo/> 
    <Plugins/> 
    <Errors/> 
</tns:phpsysinfo> 

我用http://www.webtoolkitonline.com/xml-formatter.html进行格式化。

+0

非常感谢,解决了。 –