我正在尝试通过以下SOAP请求提取从vCenter Server中提取的虚拟机名称。虽然我得到了结果(参见下面的var_dump),但我无法弄清楚如何设置循环以获取名称,因为有许多嵌入式数组,我尝试的所有内容都会返回错误或数组。我很欣赏这可能是一个非常简单的但我无法弄明白。
这是我想要获得的代码提取中的名称值。
$request->specSet = array (
'propSet' => array (
array ('type' => 'VirtualMachine', 'all' => 0, 'pathSet' => array('name','runtime.powerState', 'config.hardware.numCPU', 'config.hardware.memoryMB')),
),
PHP页面
$ss1 = new soapvar(array ('name' => 'FolderTraversalSpec'), SOAP_ENC_OBJECT, null, null, 'selectSet', null);
$ss2 = new soapvar(array ('name' => 'DataCenterVMTraversalSpec'), SOAP_ENC_OBJECT, null, null, 'selectSet', null);
$a = array ('name' => 'FolderTraversalSpec', 'type' => 'Folder', 'path' => 'childEntity', 'skip' => false, $ss1, $ss2);
$ss = new soapvar(array ('name' => 'FolderTraversalSpec'), SOAP_ENC_OBJECT, null, null, 'selectSet', null);
$b = array ('name' => 'DataCenterVMTraversalSpec', 'type' => 'Datacenter', 'path' => 'vmFolder', 'skip' => false, $ss);
$res = null;
try
{
$request = new stdClass();
$request->_this = $ret->propertyCollector;
$request->specSet = array (
'propSet' => array (
array ('type' => 'VirtualMachine', 'all' => 0, 'pathSet' => array('name','runtime.powerState', 'config.hardware.numCPU', 'config.hardware.memoryMB')),
),
'objectSet' => array (
'obj' => $ret->rootFolder,
'skip' => false,
'selectSet' => array (
new soapvar($a, SOAP_ENC_OBJECT, 'TraversalSpec'),
new soapvar($b, SOAP_ENC_OBJECT, 'TraversalSpec'),
),
)
);
$res = $client->__soapCall('RetrieveProperties', array((array)$request));
}
catch (Exception $e)
{
echo $e->getMessage();
}
的var_dump()
object(stdClass)#46 (1) {
["returnval"]=>
array(2) {
[0]=>
object(stdClass)#47 (2) {
["obj"]=>
object(stdClass)#48 (2) {
["_"]=>
string(5) "vm-35"
["type"]=>
string(14) "VirtualMachine"
}
["propSet"]=>
array(4) {
[0]=>
object(stdClass)#49 (2) {
["name"]=>
string(24) "config.hardware.memoryMB"
["val"]=>
int(128)
}
[1]=>
object(stdClass)#50 (2) {
["name"]=>
string(22) "config.hardware.numCPU"
["val"]=>
int(1)
}
[2]=>
object(stdClass)#51 (2) {
["name"]=>
string(4) "name"
["val"]=>
string(7) "test123"
}
[3]=>
object(stdClass)#52 (2) {
["name"]=>
string(18) "runtime.powerState"
["val"]=>
string(10) "poweredOff"
}
}
}
[1]=>
object(stdClass)#53 (2) {
["obj"]=>
object(stdClass)#54 (2) {
["_"]=>
string(5) "vm-36"
["type"]=>
string(14) "VirtualMachine"
}
["propSet"]=>
array(4) {
[0]=>
object(stdClass)#55 (2) {
["name"]=>
string(24) "config.hardware.memoryMB"
["val"]=>
int(128)
}
[1]=>
object(stdClass)#56 (2) {
["name"]=>
string(22) "config.hardware.numCPU"
["val"]=>
int(1)
}
[2]=>
object(stdClass)#57 (2) {
["name"]=>
string(4) "name"
["val"]=>
string(7) "test456"
}
[3]=>
object(stdClass)#58 (2) {
["name"]=>
string(18) "runtime.powerState"
["val"]=>
string(10) "poweredOff"
}
}
}
}
}
答案 0 :(得分:1)
我知道这是一个相对简单的修复。我在代码中添加了以下内容以获取所需的值。
$tvCPU = 0;
$tvRAM = 0;
echo "<table><tr><td width='450px'>Name</td><td width='100px'>vRAM (MB)</td><td width='100px'>vCPU</td><td width='100px'>Power State</td></tr>";
$arrlength=count($res ->returnval);
for($x=0;$x<$arrlength;$x++) {
$name = $res -> returnval[$x] -> propSet[2]->val;
$vRam = $res -> returnval[$x] -> propSet[0]->val;
$vCPU = $res -> returnval[$x] -> propSet[1]->val;
$pState = $res -> returnval[$x] -> propSet[3]->val;
echo "<tr><td width='450px'>".$name."</td><td>".$vRam."</td><td>".$vCPU."</td><td>".$pState."</td> </tr>";
$tvCPU = $tvCPU + $vCPU;
$tvRAM = $tvRAM + $vRam;
}
echo "<tr><td width='450px'>Total VMs - ".$x."</td> <td>". $tvRAM."</td> <td>". $tvCPU."</td> </tr>";