Problem with returning array of complexType

Help
2013-05-29
2014-03-03
  • Tomasz Fidos
    Tomasz Fidos
    2013-05-29

    Hi all.
    I have a problem with returning an array od complexTypes.

    My server is:

    <?php
        function Method1()
        {
            $result = array();
            $result[] = array('index' => 1, 'size' => 'small');
            $result[] = array('index' => 2, 'size' => 'big');
            $result[] = array('index' => 3, 'size' => 'medium');
            $result[] = array('index' => 4, 'size' => 'small');
            $result[] = array('index' => 5, 'size' => 'small');
            $result[] = array('index' => 6, 'size' => 'big');
            return $result;     
        }
    /////////////////////////////////////////////////////////////////////////////////////
        
        
        
        require_once("nusoap/lib/nusoap.php");
        $namespace = "http://localhost/api/tstserver.php";
        // create a new soap server
        $server = new soap_server();
        $server->configureWSDL("WSDLTST");
        $server->soap_defencoding = 'UTF-8'; 
        $server->wsdl->schemaTargetNamespace = $namespace;
            
    $server->wsdl->addComplexType(
        'Product',
        'complexType',
        'struct',
        'all',
        '',
        array(
            'index' => array('name'=>'index','type'=>'xsd:int'),
            'size' => array('name'=>'size','type'=>'xsd:string')
        )
    );
    $server->wsdl->addComplexType(
        'ProductArray',
        'complexType',
        'array',
        '',
        'SOAP-ENC:Array',
        array(),
        array(
            array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:Product[]')
        ),
        'tns:Product'
    );
         
            
        $server->register(
               'Method1',
               array(),
               array('return'=>'tns:ProductArray'),
               $namespace,false,'rpc','encoded','bla bla bla');
                  
                  
                  
        $server->service(isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '');
        exit(); 
        
          
    ?>
    

    My client in php is:

    <?php
        require_once('nusoap/lib/nusoap.php');
        $client = new soapclient('http://localhost/api/tstserver.php?wsdl');
        
        $result = $client->Method1();
        
        echo "<pre>";
        print_r($result);
        echo "</pre>";
    ?>
    

    Output from my client is

    Fatal error: Uncaught SoapFault exception: [Client] looks like we got no XML document in C:\XAMPP\htdocs\api\tstclient.php:6 Stack trace: #0 C:\XAMPP\htdocs\api\tstclient.php(6): SoapClient->__call('Method1', Array) #1 C:\XAMPP\htdocs\api\tstclient.php(6): SoapClient->Method1() #2 {main} thrown in C:\XAMPP\htdocs\api\tstclient.php on line 6
    
     
  • Tomasz Fidos
    Tomasz Fidos
    2013-05-30

    Anyone, please? :)

    I'm using Nusoap 0.9.5 with XAMPP 1.8.1 (php 5.4.7).

    I can't find any answer for my question :/
    Due to articles and tutorials from the internet my code should work, but it's now. Please help.

     
  • Tomasz Fidos
    Tomasz Fidos
    2013-06-03

    OK, I handled it.

    If you are using PHP 5.4 you have to comment line 6132 in nusoap.php.

     
    • JohnH
      JohnH
      2013-10-09

      You are a mad genius sir! Would have taken me forever to figure out why my WSDL was correct, yet I couldn't get access to my object array within an object array. Absolutely awesome, you've saved me a lot of time since our servers run 5.4 , can't tell you how much!

       
  • manoj
    manoj
    2013-07-30

    Hi Tomasz,

    when I test this function it's returing correctly, but when I fetch data from mysql and return an array it's returning only

    Array
    (
    [0] => stdClass Object
    (
    )

    )

    not showing restult how can I do this?

    my php code is:

    //server.php

    /*
    * @author
    * @copyright 2013
    /

    function MyFunction()
    {
        $temparray = array();
        //$temparray[] = array('index' => 1, 'size' => 'small');
        //return $temparray;
        mysql_connect('localhost','root','') or die(mysql_errno());
        mysql_select_db('Sample') or die(mysql_errno());
    
        $myquery = 'SELECT * FROM books';
    
        $result = mysql_query($myquery) ;
    
        if(!$result)
        {
            $temparray[] = array('Error' =>'Database Error');
            return $temparray;
        }
        else{
            while($row = mysql_fetch_assoc($result)){
                $temparray[] = array('BookId' => $row['id'], 'BookTitle' => $row['title'],'BookAuthor' => $row['author'],'BookPrice' => $row['price']);
    
        }
    
         }
    
        return $temparray;
    
    }
    

    /* function Method1()
    {
    $result = array();
    $result[] = array('index' => 1, 'size' => 'small');
    $result[] = array('index' => 2, 'size' => 'big');
    $result[] = array('index' => 3, 'size' => 'medium');
    $result[] = array('index' => 4, 'size' => 'small');
    $result[] = array('index' => 5, 'size' => 'small');
    $result[] = array('index' => 6, 'size' => 'big');
    return $result;
    }

     function Method2()
    {
        $result = array();
        for ($i=1; $i<=5; $i++)
       {
           $result[] = array('index' => $i, 'size' => 'small$i');
       }
    
        return $result;     
    }*/
    

    /////////////////////////////////////////////////////////////////////////////////////

    require_once("nuSOAP/lib/nusoap.php");
    $namespace = "http://localhost/api/tstserver.php";
    // create a new soap server
    $server = new soap_server();
    $server->configureWSDL("WSDLTST");
    $server->soap_defencoding = 'UTF-8';
    $server->wsdl->schemaTargetNamespace = $namespace;

    $server->wsdl->addComplexType(
    'Product',
    'complexType',
    'struct',
    'all',
    '',
    array(
    'index' => array('name'=>'index','type'=>'xsd:int'),
    'size' => array('name'=>'size','type'=>'xsd:string')
    )
    );
    $server->wsdl->addComplexType(
    'ProductArray',
    'complexType',
    'array',
    '',
    'SOAP-ENC:Array',
    array(),
    array(
    array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:Product[]')
    ),
    'tns:Product'
    );

    $server->register(
           'MyFunction',
           array(),
           array('return'=>'tns:ProductArray'),
           $namespace,false,'rpc','encoded','bla bla bla');
    

    / $server->register(
    'Method2',
    array(),
    array('return'=>'tns:ProductArray'),
    $namespace,false,'rpc','encoded','bla bla bla');

    /

    $server->service(isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '');
    

    exit();

    // $res = MyFunction();

    //var_dump($res);

    ?>

    //client.php

    /*
    * @author
    * @copyright 2013
    /

    require_once('nuSOAP/lib/nusoap.php');
    $client = new soapclient('http://localhost:8080/myfiles_bojjaiah/examples/final/Server.php?wsdl');

    //$result = $client->Method1();
    //$result1 = $client->Method2();
    $result = $client->MyFunction();
    
    echo "<pre>";
    print_r($result);
    echo "</pre>";
    

    // echo "

    ";
    // print_r($result1);
    // echo "
    ";

    ?>

    my result is:

    Array
    (
    [0] => stdClass Object
    (
    )

    [1] => stdClass Object
        (
        )
    

    )

    here not showing result why?

    could you tell me where I went wrong?

    thanks in advacne.............

     
  • Philippe
    Philippe
    2014-03-03

    Hi,

    when renaming "Method1" to "Method2" or whatever, in server.php and client.php, the script doesn't work anymore. Do I have to change something else ?

    Regards
    Phil

     
    • mango
      mango
      2014-03-03

      <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=gb2312"> </HEAD>

      <BODY>

      官方阿迪达斯新店开张大促销了,三折呀,还有情侣款,我卖了两双,去验过是正品,很满意~!http://st.zzaa.org


      This is an automatic reply, confirming that your e-mail was received.Thank you



      邮箱使用小提示


      想让对方更及时看到你的邮件,可以试试发到"对方手机号@163.com"。



      无需事先开通,还有短信提醒。了解网易手机邮箱详情


      </body>
      </html>