Showing posts with label nusoap. Show all posts
Showing posts with label nusoap. Show all posts

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!

How to write SOAP web service using PHP?


In my previous post i have explained "How to read gmail inbox using PHP". Today I am going to write on Web service using SOAP. If you are a newbie and have a question in mind that "How to create a SOAP service?" this article will be useful to get started. Here are few easy steps to follow.

[NB: If you are facing trouble with below code then please do visit my latest updated post "SOAP web service using PHP". ]

Step 1 :

Download nusoap library file from here. Unzip and copy the folder into www (WAMP) root folder. If you are using Xampp tool paste it into htdocs/xampp/ folder.

Step 2 :

Now lets create two php file one is server.php and another client.php.

Step 3 :

Copy below snippet into your server.php.

 
     require_once('lib/nusoap.php');
 $server = new nusoap_server;
/*
//require("Connection.class.php"); 
$server ->configureWSDL('server', 'urn:server');
$server ->wsdl->schemaTargetNamespace = 'urn:server';
$server ->register('mpollServer', array('value' => 'xsd:string'), 
                    array('return' => 'xsd:string'), 'urn:server', 'urn:server#mpollServer');
*/

//register a function that works on server
$server ->register('getfeedDetails', array('value' => 'xsd:string'), 
                   array('return' => 'xsd:string'), 'urn:server', 'urn:server# getfeedDetails');

// create the function to fetch Data’s from Database
function getfeedDetails ()
{
   $conn = mysql_connect('localhost','root','');
   mysql_select_db('news', $conn);
   $sql = "SELECT * FROM user_story";
   $q = mysql_query($sql);
   $items = array();
   while($row = mysql_fetch_array($q)){

       $items [] = array(
            'story_url'=> $row['story_url'],
            'story_title'=> $row['story_title'],
            'story_description'=> $row['story_description'],
            'story_image'=> $row['story_image']
       );
    }
   return $items;
}

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


      

Above code snippet will help your to access database and fetch details.

Step 4 :

Now let us open client.php and paste below code snippet into it. You have to create a client object and call your server method to retrieve the results from server.

require_once('lib/nusoap.php');
$url = "http://localhost/testserver/server.php?wsdl";
$client = new nusoap_client($url);
//Call server function
$response = $client ->call(' getfeedDetails ');

if($client->fault)
{
    echo "FAULT: Code: (".$client ->faultcode.")";
    echo "String: ".$client->faultstring;exit;
}
else
{
                $result = $response;
                $count = count($result);
?>
    >
                <table>
    <tbody><tr>
                <th>Story Url</th>
                <th>Story Title</th>
                <th>Story Description</th>
                <th>Story Image</th>
    </tr>
    
    <tr echo="" php="" rowtype=""><td></td>
                <td></td>
                <td></td>
                <td><img src="<?php echo $result[$i]['story_image']?>" type="photo" /> </td>
    </tr>
    
    </tbody></table>
 <php } ?>   
   
<style type="text/css">
    th {
        background:#007F99;
        color:#fff;
    }
</style>

      

Step 5 :

Thats it! We are done. Now go to browser and run client.php. It will call the SOAP server and fetch you the details from database.

Isn't it easy? Leave your comments below and don't forget to share this article with your friends. Thanks for reading.

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.