skip to main | skip to sidebar

php web service return array


what is web services in php ?

Nusoap is php classes library that is developed by free php script nusoap .A web services that is created with the help of nusoap php classes is called Nusoap web service.Nusoap lib is Free php-script which you can use for web service.you can learn creating web services using php.Creating web service in php as easy as other code you write in php.Here you will learn how to creare web service in php.This is php web service example using nusoap lib.

web services using php.

  1. Create new folder and rename it to "server" on your server space
  2. ( local user just create in htdocs. )
  3. Download Nusoap library from
  4. nusoap it is zip file
  5. You have to extract the "lib" folder from it and paste to web root(I.E htdocs).
  6. Now create new php file in sever folder and rename it to "server.php"
  7. Paste the following code in it.
  8. server.php
    error_reporting(0);
     //include required class for build nnusoap web service server
     require_once('../lib/nusoap.php');
     // Create server object
     $server = new soap_server();
     // configure WSDL
     $server->configureWSDL('PHP Web Services return array', 'urn:returnArray');
     // Complex Type Struct for return array
    $server->wsdl->addComplexType('array_php',
     'complexType',
     'struct',
     'all',
     '',
     array(
     'id' => array('id' => 'id', 'type' => 'xsd:string'),
     'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'),
     'lastname' => array('name' => 'lastname', 'type' => 'xsd:string'),
     'email' => array('name' => 'email', 'type' => 'xsd:string')
     )
     );
     $server->wsdl->addComplexType('return_array_php',
     'complexType',
     'array',
     'all',
     'SOAP-ENC:Array',
     array(),
     array(
     array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:array_php[]')
     ),
     'tns:array_php'
     );
     
     $server->register('get_data',
     array('limit_start' => 'xsd:int','limit_end' => 'xsd:int'),
     array('return' => 'tns:return_array_php'),
     $ns,
     $ns.'#get_data',
     'rpc',
     'encoded',
     'Returns array data in php web service'
     );
     
     function get_data($limit_start,$limit_end) 
     {
     
     /* you can use mysql and your logic here
     this is sample array */
     
     $array_rtr=array();
     $array_rtr[0]['id']=0;
     $array_rtr[0]['firstname']='Nikunj';
     $array_rtr[0]['lastname']='Gandhi';
     $array_rtr[0]['email']='Nik_gandhi007@yahoo.com';
     
     $array_rtr[1]['id']=1;
     $array_rtr[1]['firstname']='ABC';
     $array_rtr[1]['lastname']='EGF';
     $array_rtr[1]['email']='ABC@yahoo.com';
     
     $array_rtr[2]['id']=2;
     $array_rtr[2]['firstname']='XYZ';
     $array_rtr[2]['lastname']='ZYX';
     $array_rtr[2]['email']='XYZ@yahoo.com';
     
     $array_rtr[3]['id']=3;
     $array_rtr[3]['firstname']='zcds';
     $array_rtr[3]['lastname']='asdsa';
     $array_rtr[3]['email']='dxds@yahoo.com';
     
     $array_rtr[4]['id']=4;
     $array_rtr[4]['firstname']='zxyctuzy';
     $array_rtr[4]['lastname']='zxkjch';
     $array_rtr[4]['email']='xyx@yahoo.com';
     
     $array_rtr[5]['id']=5;
     $array_rtr[5]['firstname']='sdd';
     $array_rtr[5]['lastname']='dss';
     $array_rtr[5]['email']='dss@yahoo.com';
     
     $array_rtr[6]['id']=6;
     $array_rtr[6]['firstname']='sa';
     $array_rtr[6]['lastname']='aZz';
     $array_rtr[6]['email']='axz@yahoo.com';
     
     $array_rtr[7]['id']=7;
     $array_rtr[7]['firstname']='xxz';
     $array_rtr[7]['lastname']='xcx';
     $array_rtr[7]['email']='zxz@yahoo.com';
     
     $array_rtr[8]['id']=8;
     $array_rtr[8]['firstname']='zxx';
     $array_rtr[8]['lastname']='eee';
     $array_rtr[8]['email']='eee@yahoo.com';
     
     $array_rtr[9]['id']=9;
     $array_rtr[9]['firstname']='xxxs';
     $array_rtr[9]['lastname']='ssa';
     $array_rtr[9]['email']='sssw@yahoo.com';
     
     $return=array();
     for($i=$limit_start;$i<=$limit_end;$i++){
     
     $return[$i]=$array_rtr[$i]; 
     
     }
     
     return $return;
     }
     
     
     $server->service($HTTP_RAW_POST_DATA);
    
Now Client Side Steps:
  1. Now create new php file in sever folder and rename it to "client.php"
  2. Paste the following code in it.
  3. client.php
    error_reporting(0);
     // includes nusoap class
    require_once('../lib/nusoap.php');
     
    // Create object
    //change according your server settings
    $client = new nusoap_client('http://localhost:8081/web_service-return_array/server/server.php?wsdl', true);
    //use curl --set here to true
    //$client->use_curl=true;
    // Check for an error
    $err = $client->getError();
    if ($err) {
     // error if any
     echo '
    Constructor error
    
    ' . $err . '
    '; } $result = $client->call('get_data', array('limit_start' => 0,'limit_end'=>5),'','','',true); // fault if any if ($client->fault) { echo ' Fault
    ';
     print_r($result);
     echo '
    '; } else { // Check for errors $err = $client->getError(); if ($err) { // Display the error echo 'Error';
    ' . $err . '
    '; } else { // Display the result if($result!=false) { echo "Results"; foreach($result as $key=>$val) { //go through array print_r($val); } } } }

Buy Me a Beer-If you are like this post

If you does not have paypal account create free account create account

No comments:

Post a Comment

[フレーム]

Subscribe to: Post Comments (Atom)
 

AltStyle によって変換されたページ (->オリジナル) /