我一直在努力从apis中获取数组。不知道为什么我不能把它包裹起来。
这一次,我似乎无法访问密钥和结果。
我使用curl来获取json数组结果。但是我似乎无法正确访问它们。我一直得到php错误警告:非法字符串偏移。显然我没有访问正确的密钥。
我尝试解码和编码并以这种方式访问它们无济于事。
我已经在这里和其他地方阅读了大量的问题,但是无法让它发挥作用。
我的代码是:
<?php
$url="http://localhost/geeks/monitor/xml.php?plugin=complete&jsonp&callback=getData";
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$resultjson=curl_exec($ch);
// Closing
curl_close($ch);
$array = json_decode(json_encode($resultjson), true);
print_r($array);
?>
返回以下内容:
getData({"Generation":{"@attributes":{"version":"3.2.x","timestamp":"1446194411"}},"Options":{"@attributes":{"tempFormat":"c","byteFormat":"auto_binary","refresh":"60000","threshold":"90","showPickListTemplate":"true","showPickListLang":"true","showCPUListExpanded":"true","showCPUInfoExpanded":"false"}},"Vitals":{"@attributes":{"Hostname":"domain.domain.com","IPAddr":"127.1.1.1","Kernel":"2.6.18-404.el5 (SMP) x86_64","Distro":"CentOS release 5.11 (Final)","Distroicon":"CentOS.png","Uptime":"778721.78","Users":"0","LoadAvg":"0.82 0.83 0.81","SysLang":"English United States (en_US)","CodePage":"UTF-8","Processes":"616","ProcessesRunning":"3","ProcessesSleeping":"611","ProcessesZombie":"2","OS":"Linux"}},"Network":{"NetDevice":[{"@attributes":{"Name":"lo","RxBytes":"20523255371","TxBytes":"20523255371","Err":"0","Drops":"0","Info":"127.0.0.1"}},{"@attributes":{"Name":"eth0","RxBytes":"32510645609","TxBytes":"337131806000","Err":"0","Drops":"0","Info":"00-0C-29-7A-BB-86;202.130.34.3"}},{"@attributes":{"Name":"eth1","RxBytes":"4076707540","TxBytes":"278070","Err":"0","Drops":"0","Info":"00-0C-29-7A-BB-90"}}]},"Hardware":{"@attributes":{"Name":"VMware, Inc. VMware Virtual Platform\/440BX Desktop Reference Platform, BIOS 6.00 07\/30\/2013"},"PCI":{"Device":[{"@attributes":{"Name":"Host bridge: Intel Corporation 440BX\/ZX\/DX - 82443BX\/ZX\/DX Host bridge","Count":"1"}},{"@attributes":{"Name":"PCI bridge: Intel Corporation 440BX\/ZX\/DX - 82443BX\/ZX\/DX AGP bridge","Count":"1"}},{"@attributes":{"Name":"ISA bridge: Intel Corporation 82371AB\/EB\/MB PIIX4 ISA","Count":"1"}},{"@attributes":{"Name":"IDE interface: Intel Corporation 82371AB\/EB\/MB PIIX4 IDE","Count":"1"}},{"@attributes":{"Name":"Bridge: Intel Corporation 82371AB\/EB\/MB PIIX4 ACPI","Count":"1"}},{"@attributes":{"Name":"System peripheral: VMware Virtual Machine Communication Interface","Count":"1"}},{"@attributes":{"Name":"VGA compatible controller: VMware SVGA II Adapter","Count":"1"}},{"@attributes":{"Name":"SCSI storage controller: LSI Logic \/ Symbios Logic 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI","Count":"1"}},{"@attributes":{"Name":"PCI bridge: VMware PCI bridge","Count":"1"}},{"@attributes":{"Name":"PCI bridge: VMware PCI Express Root Port","Count":"32"}},{"@attributes":{"Name":"Ethernet controller: Intel Corporation 82545EM Gigabit Ethernet Controller","Count":"1"}},{"@attributes":{"Name":"Ethernet controller: VMware VMXNET3 Ethernet Controller","Count":"1"}}]},"IDE":{"Device":{"@attributes":{"Name":"hdc: VMware Virtual IDE CDROM Drive","Count":"1"}}},"SCSI":{"Device":{"@attributes":{"Name":"VMware Virtual disk (Direct-Access)","Count":"1"}}},"CPU":{"CpuCore":[{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}},{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}},{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}},{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}},{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}},{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}},{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}},{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}},{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}},{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}},{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}},{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}},{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}},{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}},{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}},{"@attributes":{"Model":"Intel(R) Xeon(R) CPU E5-2620 v2 @ 2.10GHz","CpuSpeed":"2100.000","Cache":"15728640","Bogomips":"4200.00"}}]}},"Memory":{"@attributes":{"Free":"60383232","Used":"15709798400","Total":"15770181632","Percent":"100"},"Details":{"@attributes":{"App":"4206075904","AppPercent":"27","Buffers":"864247808","BuffersPercent":"6","Cached":"10639474688","CachedPercent":"67"}},"Swap":{"@attributes":{"Free":"6190227456","Used":"50888704","Total":"6241116160","Percent":"1"},"Mount":{"@attributes":{"MountPointID":"1","FSType":"","Name":"SWAP","Free":"6190227456","Used":"50888704","Total":"6241116160","Percent":"1","MountPoint":"\/dev\/mapper\/VolGroup00-LogVol01"}}}},"FileSystem":{"Mount":[{"@attributes":{"MountPointID":"1","FSType":"ext3","Name":"\/dev\/mapper\/VolGroup00-LogVol00","Free":"77318148096","Used":"666516889600","Total":"784311173120","Percent":"85","MountOptions":"rw, usrjquota=quota.user, jqfmt=vfsv0","Inodes":"3","MountPoint":"\/"}},{"@attributes":{"MountPointID":"2","FSType":"ext3","Name":"\/dev\/sda1","Free":"70255616","Used":"27912192","Total":"103512064","Percent":"27","MountOptions":"rw","Inodes":"1","MountPoint":"\/boot"}},{"@attributes":{"MountPointID":"3","FSType":"tmpfs","Name":"tmpfs","Free":"7885090816","Used":"0","Total":"7885090816","Percent":"0","MountOptions":"rw","Inodes":"1","MountPoint":"\/dev\/shm"}}]},"MBInfo":{},"UPSInfo":{},"Plugins":{},"Errors":{}})
正如你所看到的,它并不打印“友好”。因此,如果有人知道使这种印刷品更具人性可读性的最佳方法,那也会很棒。但仍然不是问题。
那么我怎样才能访问它们并将每个变量放入php变量中。
我尝试了很多不同的方法,显然我没有正确选择密钥。
答案 0 :(得分:1)
你可以这样读你的json:
$array['Generation']['@attributes']['version']
o / p =&gt; 3.2.x中 您可以在http://jsonlint.com/
以适当的方式阅读您的json答案 1 :(得分:0)
json_decode($ json,true);
Array
(
[Generation] => Array
(
[@attributes] => Array
(
[version] => 3.2.x
[timestamp] => 1446194411
)
)
echo attr
echo $array["Generation"]["@attributes"]["timestamp"];
答案 2 :(得分:0)
喔!结束了它。从头开始并更改为file_get_contents。
然后我抓住它并宣布utf8并从中取出垃圾。
然后我确定我声明json_decode为true所以它将在数组中正确。
现在我可以轻松访问我需要的内容。下面是我的最终代码列表所有作为一个数组,也是一个只访问我需要的版本(版本):
<?php
$url="http://localhost/monitor/xml.php?plugin=complete&json&callback=getData";
$html = file_get_contents($url);
$json = preg_replace('/,\s*([\]}])/m', '$1', utf8_encode($html));
$json = json_decode($json, true);
echo '<p>Version : '.$json['Generation']['@attributes']['version'].'';
print_r($json);
?>