Connecting to vSphere API via PHP5 using SOAP

Connecting to VSphere API

Hey all,

After searching the internet for a bit, I wasn’t able to find a true and tried “perfect” way of connecting to vSphere API using PHP5/SOAP. Continuing my research, reading known issue guides from VMware, and scouring the web, I was finally able to connect with the script I wrote below. This basic login script should get you going, and you can reference anything else from the vSphere SDK documetentation itself.

Before we get into the code though, there is another issue we need to address first. On our version of vCenter, I was getting a SOAP error regarding reflect-types. To resolve this I downloaded the latest vSphere SDK (VMware-vSphere-SDK-5.1.0-774886.zip) from VMware to my vCenter server. Once complete, open up the vSphere SDK zip and navigate to SDK/vsphere-ws/wsdl/vim25/. Copy the reflect-messagetypes.xsd and reflect-types.xsd to this path:

C:ProgramDataVMwareVMware VirtualCenterdocRootsdk

Once I placed those 2 files in the SDK root on vCenter, I was no longer getting my SOAP exception. This is a known issue, and you can read more about it here.

And now, the moment you’ve been waiting for… here is my code sample to connect to the vSphere API via PHP5 using SOAP:

// Update $client to reflect your own IP or DNS name instead of 'vcenter'
// Update $soap_message["userName"]
// Update $soap_message["password"]

try {                                           
	$client = new SoapClient("https://vcenter/sdk/vimService.wsdl", array("location"=>"https://vcenter/sdk/"));

	// setup service content
	$soap_message["_this"] = new SoapVar("ServiceInstance", XSD_STRING, "ServiceInstance");
	$result = $client->RetrieveServiceContent($soap_message);
	$service_instance = $result->returnval;
	print_r($service_instance);

	// login example via serviceinstance
	$soap_message["_this"] = $service_instance->sessionManager;
	$soap_message["userName"] = "domainusername";
	$soap_message["password"] = "password";
	$result = $client->Login($soap_message);
	$soap_session = $result->returnval;
	print_r($soap_session);

	// logout example
	$soap_message["_this"] = $service_instance->sessionManager;
	$result = $client->Logout($soap_message); 
} catch (Exception $e) {                          
	printf("%sn",$e->__toString());
	return false;                             
} 

If all went well, you should get an output like so:

stdClass Object
(
    [rootFolder] => stdClass Object
        (
            [_] => group-d1
            [type] => Folder
        )

    [propertyCollector] => stdClass Object
        (
            [_] => propertyCollector
            [type] => PropertyCollector
        )

    [viewManager] => stdClass Object
        (
            [_] => ViewManager
            [type] => ViewManager
        )

    [about] => stdClass Object
        (
            [name] => VMware vCenter Server
            [fullName] => VMware vCenter Server 5.0.0 build-455964
            [vendor] => VMware, Inc.
            [version] => 5.0.0
            [build] => 455964
            [localeVersion] => INTL
            [localeBuild] => 000
            [osType] => win32-x64
            [productLineId] => vpx
            [apiType] => VirtualCenter
            [apiVersion] => 5.0
            [instanceUuid] => REDACTED
            [licenseProductName] => VMware VirtualCenter Server
            [licenseProductVersion] => 5.0
        )

    [setting] => stdClass Object
        (
            [_] => VpxSettings
            [type] => OptionManager
        )

    [userDirectory] => stdClass Object
        (
            [_] => UserDirectory
            [type] => UserDirectory
        )

    [sessionManager] => stdClass Object
        (
            [_] => SessionManager
            [type] => SessionManager
        )

    [authorizationManager] => stdClass Object
        (
            [_] => AuthorizationManager
            [type] => AuthorizationManager
        )

    [perfManager] => stdClass Object
        (
            [_] => PerfMgr
            [type] => PerformanceManager
        )

    [scheduledTaskManager] => stdClass Object
        (
            [_] => ScheduledTaskManager
            [type] => ScheduledTaskManager
        )

    [alarmManager] => stdClass Object
        (
            [_] => AlarmManager
            [type] => AlarmManager
        )

    [eventManager] => stdClass Object
        (
            [_] => EventManager
            [type] => EventManager
        )

    [taskManager] => stdClass Object
        (
            [_] => TaskManager
            [type] => TaskManager
        )

    [extensionManager] => stdClass Object
        (
            [_] => ExtensionManager
            [type] => ExtensionManager
        )

    [customizationSpecManager] => stdClass Object
        (
            [_] => CustomizationSpecManager
            [type] => CustomizationSpecManager
        )

    [customFieldsManager] => stdClass Object
        (
            [_] => CustomFieldsManager
            [type] => CustomFieldsManager
        )

    [diagnosticManager] => stdClass Object
        (
            [_] => DiagMgr
            [type] => DiagnosticManager
        )

    [licenseManager] => stdClass Object
        (
            [_] => LicenseManager
            [type] => LicenseManager
        )

    [searchIndex] => stdClass Object
        (
            [_] => SearchIndex
            [type] => SearchIndex
        )

    [fileManager] => stdClass Object
        (
            [_] => FileManager
            [type] => FileManager
        )

    [virtualDiskManager] => stdClass Object
        (
            [_] => virtualDiskManager
            [type] => VirtualDiskManager
        )

    [snmpSystem] => stdClass Object
        (
            [_] => SnmpSystem
            [type] => HostSnmpSystem
        )

    [vmProvisioningChecker] => stdClass Object
        (
            [_] => ProvChecker
            [type] => VirtualMachineProvisioningChecker
        )

    [vmCompatibilityChecker] => stdClass Object
        (
            [_] => CompatChecker
            [type] => VirtualMachineCompatibilityChecker
        )

    [ovfManager] => stdClass Object
        (
            [_] => OvfManager
            [type] => OvfManager
        )

    [ipPoolManager] => stdClass Object
        (
            [_] => IpPoolManager
            [type] => IpPoolManager
        )

    [dvSwitchManager] => stdClass Object
        (
            [_] => DVSManager
            [type] => DistributedVirtualSwitchManager
        )

    [hostProfileManager] => stdClass Object
        (
            [_] => HostProfileManager
            [type] => HostProfileManager
        )

    [clusterProfileManager] => stdClass Object
        (
            [_] => ClusterProfileManager
            [type] => ClusterProfileManager
        )

    [complianceManager] => stdClass Object
        (
            [_] => MoComplianceManager
            [type] => ProfileComplianceManager
        )

    [localizationManager] => stdClass Object
        (
            [_] => LocalizationManager
            [type] => LocalizationManager
        )

    [storageResourceManager] => stdClass Object
        (
            [_] => StorageResourceManager
            [type] => StorageResourceManager
        )

    [guestOperationsManager] => stdClass Object
        (
            [_] => guestOperationsManager
            [type] => GuestOperationsManager
        )

)

stdClass Object
(
    [key] => 526fee0e-bed0-4998-3fb5-cd1a6fb0d46f
    [userName] => REDACTED
    [fullName] => REDACTED
    [loginTime] => 2012-11-09T22:28:54.730634Z
    [lastActiveTime] => 2012-11-09T22:28:54.730634Z
    [locale] => en
    [messageLocale] => en
    [extensionSession] => 
)

Another precursor not mentioned here, is getting PHP5 and SOAP to work, let alone play nice. My development box here at work is Debian 6 x64, and the built-in PHP5 SOAP module worked fine for me. Happy coding!