I'm trying to return an associative array in a PHP function. This function retrieves some data from MySQL and is published in a WebService using Zend Library.
This function looks like:
Functions.php
class Functions
{
/**
*
* @return array
*/
public function Function1()
{
$con = new mysqli(server, user, pass, database);
if ($con->connect_errno) {
die(__FUNCTION__ . $con->connect_error);
}
$con->set_charset('utf8');
$arrayRet = array();
$query = 'select field1, field2, field3 from table1';
if(!$result = $con->query($query)){
die(__FUNCTION__ . $con->error);
}
while( $row = $result->fetch_array(MYSQLI_ASSOC) )
$arrayRet[] = $row;
$result->close();
$con->close();
return $arrayRet;
}
}
Then I have another PHP file that lets me access the function via SOAP (using Zend Lib):
Server.php
<?php
include 'Zend/Soap/AutoDiscover.php';
include 'Zend/Soap/Server.php';
include 'Functions.php';
if(isset($_GET['wsdl']))
{
$autodiscover = new Zend_Soap_AutoDiscover();
$autodiscover->setClass('Functions');
$autodiscover->handle();
}
else
{
$server = new Zend_Soap_Server("http://localhost/webservice/Server.php?wsdl");
$server->setClass('Functions');
$server->handle();
}
?>
So far everything looks quite normal. If i write a php client, i'm able to consume the webservice and access the returned array as follows:
<?php
$client = new Zend_Soap_Client('http://localhost/webservice/Server.php?wsdl');
$arrayRet = $client->Function1();
foreach($arrayRet as $row )
{
?>
<b>Field1: </b><?php echo $row['field1']; ?><br>
<b>Field2: </b><?php echo $row['field2']; ?><br>
<b>Field3: </b><?php echo $row['field3']; ?><br>
<?php
}
?>
Ok, now my problem is that i'm not writing a php client but a Android client. I'm using the ksoap2 library to achieve it. With this library i'm able to handle "normal arrays" with the pare key and value, following this algorithm. Using his properies:
SoapObject category_list = (SoapObject) property;
String key = category_list.getProperty("key").toString();
String value = category_list.getProperty("value").toString();
But the response from the function above (if i copy the toString() result) looks like:
Function1Response
{
return=
[
Map{
item=anyType{key=field1; value=hello; };
item=anyType{key=field2; value=web; };
item=anyType{key=field3; value=service; };},
Map{
item=anyType{key=field1; value=hello2; };
item=anyType{key=field2; value=web2; };
item=anyType{key=field3; value=service2; };},
Map{
item=anyType{key=field1; value=hello3; };
item=anyType{key=field2; value=web3; };
item=anyType{key=field3; value=service3; };}
];
}
I could iterate this response using key and value properties, but i think it would be much better (and efficient) if i could have a response like:
Function1Response
{
return=
[
Map{
item=anyType{field1=hello; };
item=anyType{field2=web; };
item=anyType{field3=service; };},
Map{
item=anyType{field1=hello2; };
item=anyType{field2=web2; };
item=anyTypefield3=service2; };},
Map{
item=anyType{field1=hello3; };
item=anyType{field2=web3; };
item=anyType{field3=service3; };}
];
}
So i could retrive them like:
SoapObject category_list = (SoapObject) property;
String field1 = category_list.getProperty("field1").toString();
String field2 = category_list.getProperty("field2").toString();
String field3 = category_list.getProperty("field3").toString();
Is that possible? I think it can be done somehow in the php server side. But i have no idea. I have been reading here and there but nobody seems to have this problem.. or a solution.
I'm sorry for the long post. I could give more code details or explain it better if i've been not clear enough.
Thanks for helping!