VMware {code} Community
aBrunoCarrillo
Contributor
Contributor

Soap error in php whitout nusoap

Hi, i'm developing a web service to control vSphere 4 using PHP and SOAP.

I reach to get a connection using

$client = new SoapClient('https://IP/sdk/vimService.wsdl');

But when i send this function:

$client->RetrieveServiceContent(array('_this' => 'ServiceInstance'));

I get this error:

SoapClient::__doRequest(): connect() failed: Connection refused

Am I doing something wrong? or my vSphere is not configured right, or what?

Thanks

0 Kudos
3 Replies
stefanis
Contributor
Contributor

Simple answer, yes. I just got done dealing with this myself, there are really two issues. 1) The WSDL does not contain the service location, so you have to specify it yourself when you new() the soapclient. 2) The value of _this needs to be, for lack of a better term, type casted to a "ServiceInstance" object. It would appear that one would do this by:

<?php

$IP="xxx.xxx.xxx.xxx";

$vmware = new soapclient("https://$IP/sdk/vimService.wsdl",array("location"=>"https://$IP/sdk/"));

$val = $vmware->RetrieveServiceContent(array("_this" =>; new SoapVar("ServiceInstance", XSD_STRING, "ServiceInstance", "urn:vim25")));

var_dump($val);

?>

HOWEVER this results in XML that looks sort of like this:

<RetrieveServiceContent>

<_this xsi:type=urn:vim25:ServiceInstance>ServiceInstance</_this>

</RetrieveServiceContent>

The problem seems to be that vCenter doesn't like the "xsi:" prefix to the "type" parameter. And using the PHP soapclient class does not seem to allow you to get around it. What I ended up doing was to use nuSOAP 0.9.5 without the WSDL, I just couldn't make the WSDL work.

<?php

$IP="xxx.xxx.xxx.xxx";

vcenter = new nusoap_client("https://$IP/sdk/");

$soapmsg = new soapval('_this','ServiceInstance','ServiceInstance',false,false,array('type'=>'ServiceInstance'));

$result = $vcenter->call('RetrieveServiceContent',$soapmsg,"urn:vim25");

?>

Which results in XML that looks sort of like this

<RetrieveServiceContent>

<_this xsi:type=string type=urn:vim25:ServiceInstance>ServiceInstance</_this>

</RetrieveServiceContent>

NOTE that there are two "type" parameters one with the "xsi:" prefix and one without. This works. Getting that unprefixed "type" parameter into the XML is the key to making it all work. All of the calls that use an "_this" require the same sort of type casting to the correct type.

There is another issue that you will run into if you are retreiving properties or some other call that wants an array of values as input. vCenter SDK wants the array represented as a repeated key like:

<pathSet></pathSet>

<pathSet></pathSet>

<pathSet></pathSet>

nuSOAP wants to format arrays like this:

<pathSet>

<item></item>

<item></item>

<item></item>

</pathSet>

The way you work round this is again with the soapval()

foreach($pathSet as $idx => $element)

{

$soapmsg[] = new soapval('pathSet', false, $element);

}

which results in the correct format, but is not that straight forward.

Good Luck and if you do manage to get the native PHP soap client to work let me know nick AT power<remove this bit>assure com

Message was edited by: stefanis

fixed a couple of typeos

0 Kudos
anykey
Enthusiast
Enthusiast

Hi,

I just installed Fedora with PHP and the latest NuSoap 0.9.5 library and was not able to get your sample code to work. After some playing around, here is what did work for me.


<?php
date_default_timezone_set('Europe/Amsterdam');
include_once "lib/nusoap.php";

print "start";

$vcenter = new soapclient("https://192.168.1.230/sdk/");

$soapmsg[data] = new soapval('_this','ServiceInstance','ServiceInstance',false,false,array('type'=>'ServiceInstance'));

$result = $vcenter->call('RetrieveServiceContent',$soapmsg,'urn:vim25');

print "<textarea cols=120 rows=20>";
print_r ($result);

print "</textarea>";


echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($vcenter->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($vcenter->response, ENT_QUOTES) . '</pre>';

print "Done";


?>

0 Kudos
stefanis
Contributor
Contributor

I don't see any functional difference between your code and mine. You are on the right track. This same new soapval('_this',...) concept can be used to access all the other SDK functions. But as I mentioned before, you have to remember that when the SDK says "array" or uses to indicate an array you have to format the variables as repeated keys, because nuSOAP formats arrays differently.

0 Kudos