Search code examples
c#phpmysqlweb-servicesnusoap

How to call a function that returns an array from a webservice?


I am new to C# and I am using nuSOAP and PHP. I have coded a function in a web service that returns an array. The problem is that I don't know how to get that array from the client side. Here's the relevant code in my web service:

function GetSection(bool $wcoloumn,string $coloumn, bool $all){
    if($wcoloumn== true && $all==false){
    $SQL = "SELECT `$coloumn` FROM _sections";
    $result = mysql_query($SQL);

    $dataCOL = array();

    $index = 0;
    $num = mysql_num_rows($results);
        while($row = mysql_fetch_assoc($result)) // loop to give you the data in an associative array so you can use it however.
        {
            if ($num > 0) {
            // You have $row['ID'], $row['Category'], $row['Summary'], $row['Text']
            $dataCOL[$index] = $row['$coloumn'];
            $index++;
            }
        }
        return $dataCOL();
    }
    }

This is the function that returns an array [ $dataCOL(); ].

Also note that I have added my own complex type (array) :

$server->wsdl->addComplexType("ArrayOfString",                      
                 "complexType",
                 "array",
                 "",
                 "SOAP-ENC:Array",
                 array(),
                 array(array("ref"=>"SOAP-ENC:arrayType","wsdl:arrayType"=>"xsd:string[]")),
                 "xsd:string");

Also I registred this function :

$server->register('GetSection', array('wcoloumn' => 'xsd:boolean', '$coloumn' => 'xsd:string', 'all' => 'xsd:boolean'), array('result' => 'tns:ArrayOfString'));

The client side code is written in C# and currently looking like this :

public static userdatawsdl webs = new userdatawsdl();

public String[] arrSections = webs.GetSection(true, "id", false);

public String[] GetAppSections(bool wcoloumn,string coloumn)
{
   return arrSections[]; // Here I get syntax error :D
}

The error I get is only on the client side:

Syntax error; value expected

What might be the issue here?


Solution

  • Your issue here is converting the array into a string array, try using Linq

    public String[] GetAppSections(bool wcoloumn,string coloumn)
    {
    string[] foo = webs.GetSection(true, "id", false).OfType<object>().Select(o => o.ToString()).ToArray();
    return foo
    }