SOAP web service using PHP + Mysql


Introduction:

This post for people those who are facing trouble with my last article “How to create SOAP service?”. These days web service is playing vital role in most of the web or mobile based application, where cross platform application used to connect and share data using web service. I will explain with some simple steps to do this stuff.

Download


Step 1:

Create a server page called soap_server.php into http://localhost/soapserver/. Paste the below code into the soap_server.php file.



// nosoap.php library file inside your lib/ directory
require_once('lib/nusoap.php');

$server = new nusoap_server;

/*
$server->configureWSDL('server', 'urn:server');
$server->wsdl->schemaTargetNamespace = 'urn:server';
$server->register('mypollServer', 
  array('value' => 'xsd:string'), array(
        'return' => 'xsd:string'), 'urn:server', 
        'urn:server#mypollServer
'
  );
*/

//register a function that works on server
 $server->register('getInfo');

 // create age calculation function
 function find_age($birthday)
 { 
    list($byear, $bmonth, $bday) = explode('-', $birthday);
   list($cyear, $cmonth, $cday) = explode('-', date('Y-m-d'));
 
   $cday -= $bday;
   $cmonth -= $bmonth;
   $cyear -= $byear;
 
   if($cday < 0)
   $cmonth--;
   if($cmonth < 0)
   $cyear--;
 
   return $cyear;
 }

 // create the function 
 function getInfo($name, $birthday)
 {
    $result['status'] = true;

    if(!$name){
      return new soap_fault('Client','','SANJAY!');
    }


    // Return if you want only very few server response else delete this line.
    //return $result = array('name'=> $name,'age'=> find_age($birthday) );

    

    $conn = mysql_connect('localhost','root','');
    mysql_select_db('webservice', $conn);

    $sql = "SELECT * FROM books";
    $q = mysql_query($sql);

    $result = array();

    while($row = mysql_fetch_array($q)) {

       $result[] = array(
                    'cd'=>$row['cd'],
                    'title'=>$row['title'],
                    'author'=>$row['author'],
                    'publisher'=>$row['publisher']
        );

    }

   return $result;
 }

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server ->service($HTTP_RAW_POST_DATA);
exit();



Step 2:

Now let us create a page called soap_client.php and paste below code to call soap server.



require_once('lib/nusoap.php');
header('Content-type: text/html');

$client = new nusoap_client('http://localhost/soapserver/soap_server.php');
$err = $client->getError();

if ($err) {
 echo '

Constructor error

' . $err . '

';exit; } $param = array( 'name' => 'Sanjoy Dey','birthday'=>'1987-12-20'); $response = $client->call('getInfo',$param); var_dump($response); if($client->fault) { echo "FAULT: Code: (".$client->faultcode.") "; echo "String: ".$client->faultstring; } else { echo $response['name']."\n Age: ".$response['age']."'s"; } echo '<h2> Request</h2> <>' . htmlspecialchars($client-&gt;request, ENT_QUOTES) . '</br> '; echo '<h2> Response</h2> <br>' . htmlspecialchars($client-&gt;response, ENT_QUOTES) . '</br> '; //echo '<h2>Debug</h2><br>' . htmlspecialchars($client-&gt;debug_str, ENT_QUOTES) . '</br>';

Step 3:

Go to browser and run http://localhost/soapserver/soap_client.php file. You will Soap Response as "Name" and "Date of Birth".

Conclusion:

This small post help you to create basic SOAP web service using PHP and nusoap.php library. Hope this post helps to solve displaying blank page. Keep visiting and stay updated. You can also join our technical groups by clicking below links.

Good Luck!

4 comments:

  1. sir can you tell me how to execute the service
    thank you sir.

    ReplyDelete
  2. Very useful....

    ReplyDelete
  3. Sanjoy,

    Thanks for the example to retrieve data from a database using SOAP.

    Can you also show us how to send some data from a client to a service where a couple of existing items are updated in the database?

    Thanks,
    Niki

    ReplyDelete
  4. The explanation of PHP web development is good.It explained about all necessary and unnecessary items of PHP web development,and mainly described about how to create soap service???
    Web Developer in Bangalore

    ReplyDelete

Follow Us On Facebook Open Source Web Developers by Appsntech facebook group Twitter Open Source Web Developers by Appsntech twitter group Google+ Open Source Web Developers by Appsntech Google group Linkedin Open Source Web Developers by Appsntech, LinkedIn group
Copyright @2011-2015 appsntech.com. All rights reserved.