Pages

Monday, 5 August 2013

Creating and Invoking Web Service using nusoap in PHP

              
Today I am writing my web service using SOAP. If you are wondering “How to create SOAP service?” , Then solution is here. Here I have created web service. All you need to do is, you just have to call service and function to perform particular task.
So let’s follow steps to do this stuffs,

Step 1 : Download nusoap zip file from here.  And paste it inside your www folder if you are using wamp, for xampp user paste it inside htdocs/xampp folder.
Step 2:  create two file call server.php and client.php.
Step 3 :  Inside server.php write below lines of code.
 
<?php

    function MyFunction()
    {
        $temparray = array();
        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['BookId'], 'BookTitle' => $row['Title'],'BookAuthor' => $row['Author'],'BookPublisherName' => $row['PublisherName'],'BookCopyrightYear' => $row['CopyrightYear']);
        }
    }
        return $temparray;
    }
     
/////////////////////////////////////////////////////////////////////////////////////   

  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(
    'Book',
    'complexType',
    'struct',
    'all',
    '',
    array(
        'BookId' => array('name'=>'BookId','type'=>'xsd:int'),
        'BookTitle' => array('name'=>'Title','type'=>'xsd:string'),
        'BookAuthor' => array('name'=>'Author','type'=>'xsd:string'),
        'BookPublisherName' => array('name'=>'PublisherName','type'=>'xsd:string'),
        'BookCopyrightYear' => array('name'=>'CopyrightYear','type'=>'xsd:string')
    )
);
$server->wsdl->addComplexType(
    'BookArray',
    'complexType',
    'array',
    '',
    'SOAP-ENC:Array',
    array(),
    array(
        array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:Book[]')
    ),
    'tns:Book'
);
      
    $server->register(
           'MyFunction',
           array(),
           array('return'=>'tns:BookArray'),
           $namespace,false,'rpc','encoded','ClassDescription');
          
   $server->service(isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '');
   exit();
?>
 That’s it you have created your server file to access and fetch details from database.
Step 4:    Now create client.php and inside it just  write below code. Create client object and call your server function to retrieve details.
<html>
<head>
<title>Invoking Web Services in PHP</title>
</head>
<body>

<?php

 require_once('nuSOAP/lib/nusoap.php');
    $client = new soapclient('http://localhost/final/server.php?wsdl');
   
    $result = $client->MyFunction(); 
    var_dump($result);
    }
   
?>
</body>
</html>
Finally we have a result as given below.



 

No comments:

Post a Comment