Friday, January 08, 2010

WSF/PHP Code First Approach: Returning an Array of String

Here is a problem that many people have asked me how to do it. "Returning an array of string" with the code first approach. The API or WSDL generation annotation guide, http://wso2.org/project/wsf/php/2.0.0/docs/wsdl_generation_api.html contain all the things required in details. Here is an example of a service that return an array of string.
<?php

/** splitMe function
* @param string $string_to_split string to split
* (maps to the xs:string XML schema type )
* @return array of string $result split to array
*(maps to the xs:double XML schema type )
*/

function splitMe($string_to_split)
{
return array("result" => split(":", $string_to_split));

}

$operations = array("splitMe"=>"splitMe");
$opParams = array("splitMe"=>"MIXED");

$svr = new WSService(array("operations"=>$operations,
"bindingStyle"=>"doclit",
"opParams"=>$opParams));

$svr->reply();
?>

Note that the annotation corresponding to the return value.
 * @return array of spring $result split to array

This will generate an schema with an element of maxOccurs='unbounded'. Note that you can get the wsdl from the 'serviceurl?wsdl'.
<xsd:element name="splitMeResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="result" maxOccurs="unbounded" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

Now just generate a client for this service using wsdl2php and try invoke it. You will get an array of string as the response.
    $input = new splitMe();
$input->string_to_split = "a:b:c:d";

 
// call the operation
$response = $proxy->splitMe($input);
print_r($response);

2 comments:

sudo said...

Warning: XSLTProcessor::importStylesheet() [xsltprocessor.importstylesheet]: Invalid expression in /usr/lib/php5/20060613+lfs/wsf_c/scripts/dynamic_invocation/wsf_wsdl_util.php on line 166
...............
NO idea why get lots of error like XSLTProcessor::importStylesheet()....
Any idea?

dimuthu said...

Just checking, Does this happen for the samples shipped with the wsf/php?. In that case can you check whether the php xsl extension is uptodate?