2012-09-19 59 views
0

我試圖在PHP函數中返回關聯數組。該函數從MySQL中檢索一些數據,並使用Zend庫在WebService中發佈。使用Zend Lib通過SOAP發送php複雜類型數組並使用Android KSOAP2客戶端進行檢索

這個功能看起來像:

的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; 

    } 
} 

然後我還有一個PHP文件,讓我通過SOAP訪問功能(使用Zend庫):

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(); 
    } 
?> 

到目前爲止,一切看起來都很正常。如果我寫一個PHP客戶端,我能夠消耗Web服務並訪問返回的數組如下:

<?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 
    } 
?> 

好了,現在我的問題是,我不寫一個PHP的客戶端,但一個Android客戶端。我正在使用kso​​ap2庫來實現它。有了這個庫,我可以使用pare鍵和值處理「正常數組」,緊接着this algorithm。用他的性的判定:

SoapObject category_list = (SoapObject) property; 
    String key = category_list.getProperty("key").toString(); 
    String value = category_list.getProperty("value").toString(); 

但是從上面的函數的響應(如果我複製了toString()結果)看起來像:

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; };} 
    ]; 
    } 

我可以使用鍵和值的屬性重複這種反應,但我認爲這將是更好的(和有效)如果我能有這樣的迴應:

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; };} 
    ]; 
    } 

,所以我可以retrive他們喜歡:

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(); 

這可能嗎?我認爲可以在PHP服務器端以某種方式完成。但我不知道。我一直在這裏閱讀,但沒有人似乎有這個問題..或解決方案。

對於這篇較長的文章,我感到抱歉。如果我還不夠清楚,我可以給出更多的代碼細節或更好地解釋它。

感謝您的幫助!

+0

好的......我開始認爲這是不可能的。對?返回任何PHP數組,我將永遠擁有這對「價值和關鍵」,我永遠不能改變他的領域...... – weilah

回答

2

嗯,我找到了解決方案。

在PHP服務器的功能,將公佈結果的WebService的樣子:

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)){ 
      $myClass = new TestClass(); 
      $myClass->field1 = $row["field1"]; 
      $myClass->field2 = $row["field2"]; 
      $myClass->field3 = $row["field3"]; 
      array_push($arrayRet, $myClass); 
     } 


     $result->close(); 
     $con->close(); 

     return $arrayRet; 

    } 
} 

我以前創建的所有相關屬性的類,就這麼簡單:

TestClass.php

<?php 
class TestClass 
{ 
    /** 
    * @var string 
    */ 
    public $field1; 

    /** 
    * @var string 
    */ 
    public $field2; 

    /** 
    * @var string 
    */ 
    public $field3; 

} 

?> 

所以基本上我們返回的是一個數組,其中包含這個類的所有不同實例作爲條目返回。

現在,在Android客戶端(通常使用kso​​ap2庫)我收到這樣的迴應:

Function1Response 
{ 
    return= 
    [ 
    Struct{field1=hello; field2=web; field3=service; }, 
    Struct{field1=hello2; field2=web2; field3=service2; }, 
    Struct{field1=hello3; field2=web3; field3=service3;} 
    ]; 
} 

和我能夠遍歷它:

 //call 
     androidHttpTransport.call(SOAP_ACTION, envelope); 

     //envelope response 
     SoapObject result = (SoapObject) envelope.bodyIn; 

     //retrieve the first property, actually the array 
       Vector result3 = (Vector) result.getProperty(0); 

     //iterate 
     Enumeration e=result3.elements(); e.hasMoreElements();) 
     { 
      //each object from the array its an Soap Element that contanins the properties defined in the php class 
      SoapObject item=((SoapObject)e.nextElement()); 

      String field1 = item.getProperty("field1").toString(); 
      String field2 = item.getProperty("field2").toString(); 
      String field3 = item.getProperty("field3").toString(); 

      stringBuilder.append(
        "Field1: " + codi + "\n"+ 
        "Field2: "+ titol +"\n"+ 
        "Field3: "+ descr +"\n"+ 
        "******************************\n"); 
     } 

就是這樣。 ..我希望它會對別人有用。 謝謝

相關問題