PHP vSphere (5.x) programming
It has been a while I updated my PHP examples, the previous code was designed for VI3 (a long time back).
Not too much has changed, but somethings you should be aware of. PHP5 has a build in soap library, which I do not use, I like to use nusoap, so make sure you use the nusoap_client, instead of soap_client. I also have a small library that contains 2 functions, that I found where easier done by creating raw soap messages, instead of creating them with variables. One is for the most imports Retrieve Properties. I build it with a very extensive traversal spec, so it should be easily be able to find anything.
After adding the libraries to your code and of course having the nusoap files available you can easily create the connection:
$myconnection = new nusoap_client("https://".$ServerIP."/sdk"); // Change to your own server!
$namespace = "urn:vim25";
$soapmsg[data] = new soapval('_this','ServiceInstance','ServiceInstance');
$result = $myconnection->call("RetrieveServiceContent",$soapmsg,$namespace);
This first call “RetrieveServiceContent” can be done without providing any login details. It should return the verion of the vSphere or ESX host and most importantly the “root folder object”. You can easily display the result information by using this code:
The next step of course is to login to the VC server, this can be done as followed:
$soapmsg[this] = new soapval('_this','SessionManager','SessionManager');
$soapmsg[userName] = $username;
$soapmsg[password] = $password;
$result = $myconnection->call("Login",$soapmsg,$namespace);
The login request should return some basic info like the time (from the vSphere server) you are logged in. It also contains the session key that need to be used for all other following calls to use these credentials. You do not have to worry about this, as the nusoap library takes care of this, as long as you keep using the same connection variable.
One of the first things after login you probably want to do is find out information about certain objects like virtual machines, hosts, cluster, etc. All these objects have an unique Managed Object ID and you can retrieve this with probably the most complicated call of the entire API “RetrieveProperties”. This API call allows you to search objects, but requires some extreme complex filters to find objects. I wrote a simple wrapper around this and my vc_lib.php file comes with the function “Find”.
“Find” works as followed.
$soapmsg = Find($RootFolder,$ObjectType, $Details);
this will create the right soap message to search your object. For the object type you can use things like “VirtualMachine”, “HostSystem” or “ClusterComputeResource”. Check the SDK documentation or visit the Managed object browser on your VC server. You can do this by going to : https://your_vsphere_server/mob
The $Details should be an array of field names that you want to have returned of each object. Again, to find out what fields are returned you can best check your Manages Object browser to see the fields.
here an example to list all VM in your environment
In my download section you will find the file PHP vSphere 5 example code. This examples code includes:
- Nu soap library files
- VC Lib file with 2 functions “Find” and “QueryPerf”
- Sample code to list all VMs + details
- Sample code to query performance stats on objects like VMs, Hosts and Clusters