Friday, October 19, 2012

json return using soap server and soap client using php


How to get JSON response from SOAP Call in PHP
copy soap_server page like (index.php);
-----------------------------------------------------------------------
require_once('../lib/nusoap.php');

$server = new nusoap_server;

$server->configureWSDL('server', 'urn:server');

$server->wsdl->schemaTargetNamespace = 'urn:server';

$server->register('getrequest',
            array('name' => 'xsd:string'),       
            array('return' => 'xsd:string'),
            'urn:server',
            'urn:server#getrequest');

function getrequest($value,$address)
{
   $getval=array('name'=>$value,'address'=>$address);
  
   return json_encode($getval);
    
}

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';

$server->service($HTTP_RAW_POST_DATA);
?>
------------------------------------------------------------------------------------
create soap client page (get json data form soap)getdata.php
----------------------------------------------------------------------------
require_once('../lib/nusoap.php');
$client = new soapclient('http://127.0.0.1/test/nusoap/samples/index.php?wsdl');

$res=$client->__call('getrequest',array('name'=>'bikash','address'=>'teliakan'));
if($client->fault)
   {
      echo "Fault: <p>Code: {$client->faultcode}<br>";
      echo "String: {$client->faultstring}";
   }
   else
   {
    echo $res;
   }

7 comments:

  1. Replies
    1. Hi Payan Thanks for appreciation..:) if you have any other question you can ask by comments

      Delete
  2. hi,

    i got error when post data to webservices that using json

    [faultstring] => Fault occurred while processing.

    ReplyDelete
    Replies
    1. Hello please post your script what you have done. i can find out your fault. and i ll correct it.

      Delete
    2. do replace
      $res=$client->__call('getrequest',array('name'=>'bikash','address'=>'teliakan'));
      to
      $optionarray=array('name'=>'bikash','address'=>'teliakan');
      $res=$client->call('getrequest',$optionarray);

      Delete
  3. gr8. how do you convert a complextype to json? m using this code be its giving me error that it can not serialize it.

    $r=array('name'=>$name,'for'=>$for);
    return json_encode($r,JSON_FORCE_OBJECT);

    ReplyDelete
    Replies
    1. I think you need to put for return json include on top array :-> header('Content-Type: application/json');

      Delete