Skip to content

Instantly share code, notes, and snippets.

@scr34m
Created August 27, 2012 16:46
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save scr34m/3490246 to your computer and use it in GitHub Desktop.
Save scr34m/3490246 to your computer and use it in GitHub Desktop.
VMWare vSphere List VM's with PHP using webservices
<?php
class soapclientd extends soapclient
{
public $action = false;
public function __construct($wsdl, $options = array())
{
parent::__construct($wsdl, $options);
}
public function __doRequest($request, $location, $action, $version, $one_way = 0)
{
// echo '<pre>' . htmlspecialchars(str_replace(array ('<ns', '></'), array (PHP_EOL . '<ns', '>'.PHP_EOL.'</'), $request)) . '</pre>';
$resp = parent::__doRequest($request, $location, $action, $version, $one_way);
return $resp;
}
}
$client = new soapclientd('vimService.wsdl', array ('location' => 'http://10.1.0.47/sdk', 'trace' => 1));
try
{
$request = new stdClass();
$request->_this = array ('_' => 'ServiceInstance', 'type' => 'ServiceInstance');
$response = $client->__soapCall('RetrieveServiceContent', array((array)$request));
} catch (Exception $e)
{
echo $e->getMessage();
exit;
}
$ret = $response->returnval;
try
{
$request = new stdClass();
$request->_this = $ret->sessionManager;
$request->userName = 'root';
$request->password = 'abc123456';
$response = $client->__soapCall('Login', array((array)$request));
} catch (Exception $e)
{
echo $e->getMessage();
exit;
}
$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', 'guest.ipAddress', 'guest.guestState', '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();
}
echo '<pre>';
print_r($res);
@rtalavera
Copy link

Do you know how to retrieve vms from a specific cluster inside the datacenter folder

@scr34m
Copy link
Author

scr34m commented Jan 29, 2013

Sorry for late reply, i have only a sinle ESXi server so unable to test cluster retrieving.

@austinbarrington
Copy link

Hi,

Firstly thank you very much for this it works very well.
I am trying to pull back host details from the ''HostHardwareSummary' or 'HostSystem' Managed objects.
Would you know how to go about this. I cant seem to get my head around the Object/Data Object system.
I only want details like name,cpu,ram.

Thanks

@Xantios
Copy link

Xantios commented Oct 2, 2013

Thank you very much! :-D
the documentation to do this kind of stuff on VMware's side is next to nothing, so lets try to get some stuff working by having a look at your code!

@Elithrin
Copy link

Thanks you for you script. It's realy helpful !
Do you have a script to restart an virtual machine with php of vsphere api ?

@Sem03
Copy link

Sem03 commented Jun 12, 2015

Hi, i use your script but when i try to use it on a new server with a new version of php i receive this error:
PHP Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://192.168.1.100/sdk/vimService.wsdl' : failed to load external entity "https://192.168.1.100/sdk/vimService.wsdl"

I don't understand why.
you've an idea?

@Sem03
Copy link

Sem03 commented Jun 15, 2015

Solved.
if i add the code below (after trace=>1) it works:

'exceptions' => 1,
"stream_context" => stream_context_create(
array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
)
)

@ayushsri
Copy link

how can I run this code and what is that location at line 20

@ayushsri
Copy link

Please can you tell how to use this code

@notfalsedev
Copy link

notfalsedev commented Sep 9, 2020

For vSphere version 7.0 you must add a soap session cookie to the request.

The set-cookie header will be present in the response headers of the login-call.
With the value of the header you can add a "vmware_soap_session" cookie to the RetrieveProperties call.

@scr34m
Copy link
Author

scr34m commented Sep 9, 2020

@notfalsedev thanks for the comment

@Nacer-BKRM
Copy link

'exceptions' => 1,
"stream_context" => stream_context_create(
array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
)
)

Hi Seem03

I added as you recommended but. got the same error :(

$client = new soapclientd('vimService.wsdl', array ( 'location' => 'https://esxi.domain.com.fr/sdk', 'trace' => 1, 'exceptions' => 1, "stream_context" => stream_context_create( array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, ) ) ) ));

@MouadhSaadaoui
Copy link

hi guys, im struggling here
i have this error
Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'vimService.wsdl' : failed to load external entity "vimService.wsdl" in C:\xampp\htdocs\product-admin-master\view\listvms.php:14 Stack trace: #0 C:\xampp\htdocs\product-admin-master\view\listvms.php(14): SoapClient->SoapClient('vimService.wsdl', Array) #1 C:\xampp\htdocs\product-admin-master\view\listvms.php(26): soapclientd->__construct('vimService.wsdl', Array) #2 {main} thrown in
it will be a huge favor if someone help me in this .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment