note 54652 added to function.soap-soapvar-construct

From: Date: Mon, 11 Jul 2005 14:30:39 +0000
Subject: note 54652 added to function.soap-soapvar-construct
Groups: php.notes 
Request: Send a blank email to php-notes+get-91771@lists.php.net to get a copy of this message
This class is useful when dealing with the "anyType" type (generic object reference): it lets you specify the xsd type to provide "late binding" type information. Here's a really simple example: I have a .NET service that can take a string, a date, and integer, or other types, so I use the .NET "object" type. Here's an example of such a service--this one just tells me what type I passed in. (It's nice to use when checking to see if PHP passed in the type information the way .NET expects it.) //inside a service.asmx.cs file... [WebMethod] public string WhatTypeIsThis(object ObjectParameter) { return "You passed in a " + ObjectParameter.GetType().Name + ": " + ObjectParamter.ToString(); } To call this service with a string from PHP, I used this code: <?php //set up the service client using WSDL $client = new SoapClient("http://localhost/folder/service.asmx?WSDL"); //This is the variable that will be typed as an XSD string $typedVar = new SoapVar("mystring", XSD_STRING, "string", "http://www.w3.org/2001/XMLSchema"); //This is the wrapper object for incoming parameters $wrapper->ObjectParameter = $typedVar; //This is the named parameter object that will be passed in to the service $params = new SoapParam($wrapper, "WhatTypeIsThis"); //call the service with the string $result = $client->WhatTypeIsThis($params); //show the result echo $result->WhatTypeIsThisResult; ?> The output from this should be: "You passed in a String: mystring" The SOAP message that is passed in looks like this: <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://tempuri.org/"> <SOAP-ENV:Body> <ns1:WhatTypeIsThis> <ns1:ObjectParameter xsi:type="xsd:string">mystring</ns1:ObjectParameter> </ns1:WhatTypeIsThis> </SOAP-ENV:Body> </SOAP-ENV:Envelope> It's that xsi:type="xsd:string" that gives .NET the heads up that though the ObjectParameter is being passed in as an object, it is also a string. When trying other types, it's helpful to see exactly what is being sent to the service--to see the SOAP messages like the one above, use the trace option when making your SOAP client and then call the $client->__getLastRequest() function. (See http://www.php.net/soap_soapclient_getlastrequest) You may need to use try/catch constructs if you're generating errors. Another helpful function is var_dump($client->__getTypes())--it shows how PHP parsed the WSDL file to create types to pass back and forth. ---- Manual Page -- http://www.php.net/manual/en/function.soap-soapvar-construct.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54652 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54652&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54652&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54652&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54652&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54652&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54652&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54652&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#91771) next »