Friday, 4 April 2014

How to write SOAP web service using PHP?

    How to write web service using SOAP in PHP?
              
             In my previous post I had shown you “how to read gmail inbox using 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 createdwebservice. 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
require_once('lib/nusoap.php');
//require("Connection.class.php"); 
$server = new nusoap_server;
//$server ->configureWSDL('server', 'urn:server');
//$server ->wsdl->schemaTargetNamespace = 'urn:server';
//$server ->register('pollServer', array('value' => 'xsd:string'), array('return' => 'xsd:string'), 'urn:server', 'urn:server#pollServer');

//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);
?>
         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.
<?php
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: <p>Code: (".$client ->faultcode.")</p>";
                echo "String: ".$client->faultstring;
}
else
{
                $result = $response;
                $count = count($result);
?>
    <table >
    <tr>
                <th>Story Url</th>
                <th>Story Title</th>
                <th>Story Description</th>
                <th>Story Image</th>
    </tr>
    <?php

    for($i = 0;$i < $count-1;$i++) {
                  $rowtype = ($i % 2) ? "style='background:#88DAEB'": "style=background:#FFF";
        ?>
    <tr <?php echo $rowtype; ?>>
                <td><?php echo $result[$i]['story_url']?></td>
                <td><?php echo $result[$i]['story_title']?></td>
                <td><?php echo $result[$i]['story_description']?></td>
                <td><img src="<?php echo $result[$i]['story_image']?>" type="photo"></td>
    </tr>
    <?php
                }
                ?>
    </table>
    <?php
}
 ?>
<style type="text/css">
    th {
        background:#007F99;
        color:#fff;
    }
</style>

?>
Step 5:   That’s it you have done. Now run your client.php in your browser. It will call server and fetch the details from database.  
So today I have shown you how to use simple SOAP webservice and to get details from database.