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.