Sunday, February 3, 2013

PHP nusoap return multi array



<?php

   require_once('lib/nusoap.php'); // basic include.. must go at the top
  
   $SERVICE_NAMESPACE = "urn:Testing_Service"; // create a namespace to run under.
  
   $server = new soap_server(); // the soap object from the include above.

   // this has many input parameters but we only need two: the service name and the namespace
   $server->configureWSDL('Testing_Service', $SERVICE_NAMESPACE);
  
   // Register a method name with the service and make it publicly accessable.
   $server->register('Say_Hello',// method name
        array('name' => 'xsd:string'),// input parameter called name.. and it's a string.
        array('return' => 'xsd:string'),// output - one string is returned called "return"
        $SERVICE_NAMESPACE,// namespace
        $SERVICE_NAMESPACE . '#hello',// soapaction
        'rpc',// style.. remote procedure call
        'encoded',// use of the call
        'Sends a greeting with your name!'// documentation for people who hook into your service.
    );
    // here is the method you registered.. it takes in a string and returns a string!
    function Say_Hello($sName)
    {       
      return 'Hello ' . $sName . '!  Hello world!';
    }
   
    //----------------------------------------------------------------
    // complex types are like 'struct' in C#.... it's a way to bind an object with different properties and variables together.   
    $server->wsdl->addComplexType(
        'TableData', // the type's name
        'complexType', // yes.. indeed it is a complex type.
        'struct', // php it's a structure. (only other option is array)
        'all', // compositor..
        '',// no restriction
        array(
            'DateIn' => array('name'=>'DateIn','type'=>'xsd:string'),
            'Code' => array('name'=>'Code','type'=>'xsd:string'),
            'MSISDN' => array('name'=>'MSISDN','type'=>'xsd:string'),
            'Msg' => array('name'=>'Msg','type'=>'xsd:string')
            //DateIn,Code,MSISDN,Msg
        )// the elements of the structure.
    );
    // Here we need to make another complex type of our last complex type.. but now an array!
    $server->wsdl->addComplexType(
        'TableArray',//glorious name
        'complexType',// not a simpletype for sure!
        'array',// oh we are an array now!
        '',// bah. blank
        'SOAP-ENC:Array',
        array(),// our element is an array.
         array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:TableData[]')),//the attributes of our array.
        'tns:TableData'// what type of array is this?  Oh it's an array of mytable data
    );
    // Register a method name with the service and make it publicly accessable.
       $server->register('Get_Test_Data',// method name
        array(
                'MobileNo' => 'xsd:string',
                'SMSDate' => 'xsd:date'
                ),// input parameter - nothing!
        array('return' => 'tns:TableArray'),// output - object of type TableArray.
        $SERVICE_NAMESPACE,// namespace
       false,// soapaction
        'rpc',// style.. remote procedure call
        false,// use of the call
        ' Get all the data from test_table.'// documentation for people who hook into your service.
    );
   
        // here is the method you registered.. it takes in nothing and returns an array of MyTableArray
    function Get_Test_Data($MobileNo,$SMSDate)
    {
        // woah who has a super unsecure connection to his database?  This guy!
        require_once("common/config.php");
        require_once("lib/dbconnect.php");    

        // a simple select statement.
        $sql = "select
                DateIn,code,MSISDN,Msg
            from dbo.tbl_raw_msg
            where MSISDN='".$MobileNo."'
                and convert(varchar(10),DateIn,120)='".$SMSDate."'
            order by DateIn";

        //fill the output into something we can use.
        $result = mssql_query($sql) or die('Query failed: ' . mysql_error());
        // it's always nice to know how many rows we have.
       
        $rs=mssql_query($query);

        $return = array();
        while($row=mssql_fetch_assoc($result)){
            $return[] = $row;
        }       
      return $return;
    }

   
    //----------------------------------------------------------------
   
    //This processes the request and returns a result.
    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
    $server->service($HTTP_RAW_POST_DATA);
?>

No comments:

Post a Comment