2013-01-17 56 views
1

我有返回數組的數組的一個問題,我使用的NuSOAP使得在PHP Web服務,這裏是我的代碼:陣列的NuSOAP PHP Web服務的回報陣列

server.php

<?php 
    //libreria de nusoap 
    require_once ('nusoap/lib/nusoap.php'); 

    $miURL = 'http://localhost/webservice/'; 
    $server = new soap_server(); 
    $server->configureWSDL('Web Service de Pruebas', $miURL); 
    $server->wsdl->schemaTargetNamespace = $miURL; 

    $server->wsdl->addComplexType(
      'array_php', 
      'complexType', 
      'struct', 
      'all', 
      '', 
       array(
        'pk' => array('name' => 'pk', 'type' =>'xsd:int'), 
        'rol' => array('name' => 'rol', 'type' =>'xsd:string'), 
        'descripcion' => array('name' => 'descripcion', 'type' =>'xsd:string') 
       ) 
      ); 

    $server->wsdl->addComplexType(
      'array_array', 
      'complexType', 
      'array', 
      '', 
      'SOAP-ENC:Array', 
      array(), 
      array(
       array('ref' => 'SOAP-ENC:arrayType', 
        'wsdl:arrayType' => 'tns:array_php[]' 
       ) 
      ), 
      'tns:array_php' 
     ); 

    $server->register('prueba',     // method name 
     array(),       // input parameters 
     array('return' => 'tns:array_array'), // output parameters 
     $miURL,       // namespace 
     $miURL . '#prueba',     // soapaction 
     'rpc',         // style 
     'encoded',        // use 
     'Get Specific array_php'  // documentation 
    ); 

    function prueba() 
    { 
     $con = mysql_connect('localhost', 'root', '1234'); 
     mysql_selectdb('laboral', $con); 

     $sql = "SELECT * FROM roles"; 
     $q = mysql_query($sql); 

     $item = array(); 
     while($r = mysql_fetch_assoc($q)){ 
      $item[] = $r; 
     } 
     return $item; 

    } 


    if(!isset($HTTP_RAW_POST_DATA)) 
     $HTTP_RAW_POST_DATA = file_get_contents('php://input'); 

    $server->service($HTTP_RAW_POST_DATA); 
?> 

client.php

<?php 
    //libreria nusoap 
    require_once ('nusoap/lib/nusoap.php'); 

    //lineas de configuracion 
    $serverURL = 'http://localhost/webservice/ws2.php?wsdl'; 

    $cliente = new nusoap_client($serverURL); 

    //sí error de conexión: 
    $error = $cliente->getError(); 
    if($error){ 
     echo "<p> '.$error.' </p>"; 
     echo '<p style="color:red;'>htmlspecialchars($cliente->getDebug(), ENT_QUOTES).'</p>'; 
     die(); 
    } 


    echo "<br/>"; 
    $arreglo2 = $cliente->call('prueba'); 

    echo "<br/>"; 
    for($i=0; $i<3; $i++){ 
      print_r($arreglo2[$i]['pk']); 
      print_r($arreglo2[$i]['rol']); 
      print_r($arreglo2[$i]['descripcion']); 
      echo "<br/>"; 
     } 
?> 

問題是回報,無非是回到我的客戶,我不知道會發生什麼,我已經讀了很多論壇,但我不能找到一個答案,請如果有一個人知道幫助我在這裏

TY和對不起我的英語

+0

可能只是我,但我沒有看到你實際返回什麼東西。 – Peon

+0

第一個錯誤是,您沒有在while循環中爲數組分配mySQL響應的索引。告訴我你的查詢響應如何,我可以給你一個工作的例子。 (只需要查詢表的列名) – OrcHound

回答

2

這是我做過什麼:

// Complex Array Keys and Types ++++++++++++++++++++++++++++++++++++++++++ 
$server->wsdl->addComplexType('notaryConnectionData','complexType','struct','all','', 
     array(
       'id' => array('name'=>'id','type'=>'xsd:int'), 
       'name' => array('name'=>'name','type'=>'xsd:string') 
     ) 
); 
// ************************************************************************* 

// Complex Array ++++++++++++++++++++++++++++++++++++++++++ 
$server->wsdl->addComplexType('notaryConnectionArray','complexType','array','','SOAP-ENC:Array', 
     array(), 
     array(
      array(
       'ref' => 'SOAP-ENC:arrayType', 
       'wsdl:arrayType' => 'tns:notaryConnectionData[]' 
      ) 
     ) 
); 
// ************************************************************************* 

// This is where I register my method and use the notaryConnectionArray 
$server->register("listNotaryConnections", 
       array('token' => 'xsd:string'), 
       array('result' => 'xsd:bool', 'notary_array' => 'tns:notaryConnectionArray', 'error' => 'xsd:string'), 
       'urn:closingorder', 
       'urn:closingorder#listNotaryConnections', 
       'rpc', 
       'encoded', 
       'Use this service to list notaries connected to the signed-in title company.'); 

// In my function, I query the data and do: 
$list = array(); 
$results = mysql_query($query); 
while($row = mysql_fetch_assoc($results)) { 
    array_push($list, array('id' => intval($row['na_id']), 'name' => $row['agency_name'])); 
} 

return array("result" => true, "notary_array" => $list); 

// The output is: 
Array 
(
    [result] => 1 
    [notary_array] => Array 
     (
      [0] => Array 
       (
        [id] => 1 
        [name] => Agency 1 
       ) 

      [1] => Array 
       (
        [id] => 3 
        [name] => Agency 3 
       ) 

      [2] => Array 
       (
        [id] => 4 
        [name] => Agency 4 
       ) 

     ) 

    [error] => 
) 

希望這有助於。

+0

謝謝@詹姆斯,你讓我的一天! – Sajib

+0

@Sajib沒問題,很高興幫助。 – James