User login

problem return array from web service

Forums :

 

I try to create a samples web services with WSDL Generation, the code show below:

 

<?php

 /**

 *@return string $a

 */

function getDataFunction()    {

$a[] = "Movie";   

$a[] = "Music";

return $a;

}



$operations = array("getData"=>"getDataFunction");

$opParams = array("getDataFunction"=>"MIXED");



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

$svr->reply();

?>


--------------------------

 I use SoapUI to test the result, here is the soap response:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

   <soapenv:Header/>

   <soapenv:Body>

      <ns1:getDataResponse xmlns:ns1="http://www.wso2.org/php/xsd">

         <ns1:a>Movie</ns1:a>

      </ns1:getDataResponse>

   </soapenv:Body>

</soapenv:Envelope>

 ----------

the result show only "Movie" but "Music" is missing.

this PHP code is assume that "$a" are value of data from database, How do I fix this?

Thanks.

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Looks like to return array

Looks like to return array you have to use classmaps,  Here is the code worked for me.

<?php



class getDataResponse {

    /**

     * @var array of string $return

     */

    public $return;

}



class getData {

}



/**

 *@param object getData $getData

 *@return object getDataResponse $getDataResponse

*/

function getDataFunction($getData)    {



$a[] = "Movie";

$a[] = "Music";



$res = new getDataResponse();

$res->return = $a;

return $res;



}







$operations = array("getData"=>"getDataFunction");



$opParams = array("getDataFunction"=>"MIXED");



$classmap = array("getDataResponse" => "getDataResponse", "getData" => "getData");





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



$svr->reply();

?>

And it should be able to do this with the following code without using classmap



 /**

 *@return array of string $a

 */



function getDataFunction()    {
}



 

But appenrently this is not working, I will look in to this.





Dimuthu











In addition the above method

In addition the above method which uses classmap, you can change your code to the following (just note the return value) and it will work

/**

 *@return string $a

 */

function getDataFunction()    {



$a[] = "Movie";   



$a[] = "Music";



return "a" => $a;



}

how to write client code?

 I create database from MySQL, table name categories. The table look like this:

CategoryId CategoryName

 1

2

Movie

Music

 

I use payload method to return all data from table as shown below:

---------------
<?php
/**
 * @return array of string $result
 */

function getAllData()    {
    $link = mysqli_connect("localhost", "root", "root") or die ("Couldn't connect Database");
   

    mysqli_select_db($link, "vcd") or die ("Couldn't select Database");

    $sql = "select * from categories;";
    $result = mysqli_query($link, $sql);

    while ($row = mysqli_fetch_array($result))    {
        $a[] = $row["categoryname"];
        $b[] = $row["categoryid"];
    }   
     mysqli_close($link);
   
    $c = "";
    for($i = 0; $i < count($a); $i++)    {
        $c.= "<CategoryId>$b[$i]</CategoryId><CategoryName>$a[$i]</CategoryName>";
    }
$responsePayloadString = <<<XML
    <getDataResponse>
    {$c}
    </getDataResponse>
XML;

$result = new WSMessage($responsePayloadString);
    
    return $result;
}

$service = new WSService(array("operations" => array("getAllData")));
 
$service->reply();

?>

---------------

and here is the soap response from SoapUI

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Header/>
   <soapenv:Body>
      <getDataResponse>
         <CategoryId>1</CategoryId>
         <CategoryName>Movie</CategoryName>
         <CategoryId>2</CategoryId>
         <CategoryName>Music</CategoryName>
      </getDataResponse>
   </soapenv:Body>
</soapenv:Envelope>

 question:

  • How to coding the client?
  • Should I return '$result' value in array (@return array of string $result) or string (@return string $result)?
  • How to provide this service in wsdl_generation mode?


anyway, I use NetBeans6.1 to test GetAllData service and it return nothing! how to fix this?

WSF/PHP provides two way of

WSF/PHP provides two way of sending response. Here what you have done is building the response xml from your logic and return it. The other way (in wsdl mode), you can just return the object or the array.

In your case you have recurrent data of catagoryId, and catagoryName, in that case you need to wrap it in another type call 'Catagory', So here is how I may do this,

 

class Catagory {

  /**

   * @var int $id

    * (maps to xsd:int)

    */

    public $id;

 

   /**

    * @var string $name

    * (maps to xsd;string)

    */

     public $name;

}

 

/**

 * @return array of object Catagory $return

 */

function getAllData() {

   // .. the initial code



   $return = array();

   for ($i = 0; $i < count($a); $i ++) {

        $catagory = new Catagory();

        $catagory->id = a[i];

        $catagory->name = b[i];



        $return []= $catagory;

  }



  return $return;

}

To write the client code you can generate the code from the wsdl with the WSDl2PHP.php script available from wsf/php distribution, or from here, http://labs.wso2.org/wsf/php/wsdl2phptool.php



Thanks

Dimuthu



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.