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.
where i can find this require("Connection.class.php") ? its thorowinf an exception that file not found.
ReplyDeletethis file require("Connection.class.php") is missing that's y server.php is not returning database records.can u tell me where i can fint this file and y my server.php is not returning db records? i make database and table in mysql and insert some records.their is no issue in db connectivity.
ReplyDeleteHi !!! Raja,
DeleteYes sure. Actually My Connection.class.php will includes Database Connection. If you want to keep your DBconnection file seperately then just include it in server page else dont need that require("Connection.class.php"); line. I have writen separate file for database connection. So i was requiring that file. You may not need if you have done with database connection inside server file itself.
With Thanks & Regards,
Sanjay
Hi Sanjoy,
ReplyDeleteI gone through above article and find good and easy. I want to create one web services for my application where i want to create web base form, who will be store on client server but on submission form details will get insert into my database.
Can you please guide me how i can proceed for this?
Regards,
Jeet
Hi !! Jeet,
DeleteIf I am not wrong then you want to create service and with use of service you want to store form details into your database. Then just create a form and give form action in your client page and check your post values and call the service function and pass your post values to the service, register your function in service and inside your function you can do database operation to store datas into database.
Hope this will help you.
Regards,
Sanjay
very good series to understand web service
ReplyDeleteThank you Animesh. Keep visiting for good posts here.
DeleteHi Sanjoy Dey,
ReplyDeleteI am trying to execute this script on localhost but its throwing below error.
/*************
FAULT:
Code: (SOAP-ENV:Client)
String: error in msg parsing: XML error parsing SOAP payload on line 1: Space required
**************/
Please reply
Thanks
Remove all white space before and after tag. Because this same code works for me. Let me know if you face still same ...
DeleteHi,
ReplyDeleteSanjoy,
I am new for PHP and I use your code for my project. But it is throwing error:
Fatal error: require_once() [function.require]: Failed opening required 'lib/nusoap.php' (include_path='.;C:\php5\pear') in C:\wamp\www\client.php on line 2
So please guide me .
Hi ! Devendra,
DeleteFirst of all you should have nusoap library file to execute this. Which link I have provide above and second thing is you need to check your path. In your case path only wrong that is why your clien.php file not able to include the file and throwing fetal error. So check your library file and path properly...
Hope this will help you. If you face any other thing feel free to post. Good luck.
Hi,
ReplyDeletei facing some problem in web service in php.
1)how to change or add name space (prefix).
2)a:DeserializationFailed: Array
3)bad request
Hi Sanjoy,
ReplyDeleteI gone through above article and its very helpful. I have created files according to your blog but it gives me blank data while there is database table is not empty. Also i have removed blank space before and after tag.
Can you please guide me?
Regards,
Arpita
Hi !! Arpita,
DeleteYes sure I will help you out. First of all I need to know have you properly included the library file or not.. And Include the connection class which I wrote for creating mysql connection in different file. Its optional you can connect mysql inside your service itself. So first check those things working fine or not.
If all are working fine then please give me few more details about your code..
I worked with same code and works fine for me. Tested and posted here for you all..
Hi, Thanks a lot,i tried your code out and it worked perfectly,its a nice approach to learning web service.
ReplyDeleteit show blank values
ReplyDeleteHi Sanjoy,
ReplyDeleteIt is a nice example . also i need a step by step explanation of this code about how it works ?? Thanks for this article.
Hi Sanjoy,
ReplyDeleteI have the same issue......when i implement it.
FAULT:
Code: (SOAP-ENV:Client)
String: error in msg parsing: XML error parsing SOAP payload on line 1: Space required
any soultion for this...?
Hi Sanjoy
ReplyDeleteI am also seeing no data returned. If I try and go to the WSDL it says
"This service does not provide WSDL"
Can you please help?
Hello Sanjoy,
ReplyDeleteI have the same issue regarding the empty displaying. I did just as you said.. but it doesn't display any data from the database.. Can you please tell me what I am doing wrong here. Thanks..
Please have a look at the latest post "SOAP web service in php". Hope your problem will get solve. Its works fine for me. I will suggest don't just simply copy and paste the code have a look a bit.
DeleteKeep visiting for new post.
Thanks,
Hello Sanjoy,
ReplyDeleteCan you please tell me why you used two files i.e. client.php and server.php. If I want to create 5-6 web services for iphone then how can I manage?
The is the example i have shown here. By which you can have some idea how to create a web service and call. The server.php is - which will reside on may be your or some other server. And client.php by which you can call the server for service and get the details.
DeleteI not sure how exactly you are going to implement your web service, but should have some basic knowledge about the service to proceed for next then only you can optimize your code and time.
Happy to see your question here.
With Thanks & regards,
Sanjoy
Hi, How to write update query instead of select query ? ( using client page.. how to update table ? )
ReplyDeleteOhh.. i added update query.. Working..
Deletebt one question;
How can i connect one server to another server (means client page and server page is in different server) with same steps using nusoap ?
You can just change the server url path to call soap server or else you can do the same thing using curl also...
DeleteHi I am newer this site
ReplyDeleteGood Site
Hi sanjoy,
ReplyDeletei am new in soap. In my project i have to use webservices using soap. I check your example that's ok but i not understand how i send xml in request.Below are my webservices example
http://213.60.187.204:8577/DomoTelWebService.asmx?page=op&op=RoomTypeRequest&bnd=DomoTel%20Web%20ServiceSoap&tab=msg
can you please give some idea how i use this webservices in my project.
mahendra varma
Hello sanjoy,
ReplyDeleteI am new in web services topic. In my one project client need to use this webservices using soap. But i don't have any idea how to implement that xml file in your code. please check below link
http://213.60.187.204:8577/DomoTelWebService.asmx?page=op&op=RoomTypeRequest&bnd=DomoTel%20Web%20ServiceSoap&tab=msg
this is client webservices. Please check and give some idea how to implement that in php.
thanks.
This article provided excellent information about using SOAP in PHP.It should really useful thing for PHP developers.
ReplyDeleteWeb Developer in Bangalore
Hey Sanjoy Dey, ineed your help..i want to use php soap for request in xml form and get response also in xml form..can you help me out on this ??
ReplyDeletehey, do you have solution of my question or not ?
ReplyDeleteHi Harish,
DeleteYour server function which you are requesting should contain the code to convert your data into simple xml format or please follow wsdl. So that you can able to get the response in xml format.
Hope you understand what exactly you need to do. Let me know.
With Thanks & Regards,
Sanjoy Dey
http://www.appsntech.com/
I am new with both, php and webservices..so can you help me with example ??
Deletei want request like this:
DeleteApples
And response like following:
1.90
</soap:Envelope
so can you help me in achieving it, i would appreciate..if you can help me with an example..Thank You
hey..do you have solution of my problem Sanjoy??
DeletePlease help me out
Hi Harish,
DeleteSorry for late reply. I was lil busy with my work. You can simply pass params into your soap call with "ResponseType" => "Xml" It will fetch you the response as xml format.
If you are new in php then please learn basics with some example. Then proceed with next. Hope it helps.
Thanks.
thank a ton for reply..can i have your mail id for further conversation?
Delete