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

  • #1 written by Ben Jefferson
    about 4 years ago

    First up, massive thanks to you Richard for the example code.

    I am also working on a Raspberry Pi based monitor for VSphere infrastructure. Just thought I’d share one issue I have spent hours getting to the bottom of today.

    The approach outline here (using the VCenter MOB data to query health of hosts) is subject to a bug in VSphere (affecting v4 and v5) whereby the VCenter MOB data is not updated by the hosts and so becomes wildly out of date. Net result is that when you pull out power from a power supply there is no change in the values.

    This KB article refers to the problem in vSphere 4

    This post implies it exists in vSphere 5

    As does this one from Veeam…

    The VMWare and Veeam KB articles give the same solution although with greatly differing timeouts (120 vs 1200s).

    One thing to note – in vSphere5 the config file referred to has moved to /etc/vmware/vpxa/vpxa.cfg

    This fix worked for me. Do you know of a better fix? I’m slightly concerned that this will not scale and will extra load on the vcenter server.

  • #2 written by Richard Garsthagen
    about 4 years ago

    Hi Ben,

    I have not run into this issue yet, as I am mainly pulling performance data. There is a bug with that as well, as you have to specify the time very specifically of what window you want to query. But I got all that working. Have been testing it against my 2 esxi host environement and found no issues on being to heavy for VC in anyway.

    What kind of project are you working on?

  • #3 written by Andrew
    about 2 years ago

    I’m new in programming with vmware / vsphere platform, and I needed to retrive all vm’s infromation data such as ip, ram, hdd, os, etc.
    But I can’t find in the sample code where and what to write to retrive this information.
    May bee you could help me?

    Best regards,

  • #4 written by pihome
    about 1 year ago

    i m getting following error message.
    vsphere version 6.0, vcenter 6.0, windows 2012, with ms sql server.

    PHP Fatal error: Cannot redeclare class soapclient in /var/www/html/lib/nusoap.php on line 7244

  • #5 written by Lashunda
    about 10 months ago

    Have you ever thought about creating an ebook or
    guest authoring on other blogs? I have a blog based
    upon on the same topics you discuss and would love to have you share some stories/information.
    I know my readers would appreciate your work. If you are even remotely interested, feel
    free to send me an e mail.

  • #6 written by Romeo
    about 7 months ago

    I’m impressed, I must say. Seldom do I encounter a blog that’s both educative and amusing, and let me tell you,
    you have hit the nail on the head. The issue is an issue that too
    few people are speaking intelligently about. I am very happy that I stumbled across this during my search for something regarding this.