You are on page 1of 3

Articles Web Development Web Services Beginners

Creating Web Service Using PHP Within 10 Minutes


By Monotosh Roy Mon, 21 Oct 2013
4.50 (4 votes)

Download source code - 143 KB

Introduction
Today, I am trying to write up on web service functionality and also how to consume it using PHP code. Basically, it is a very beginner level web service tip but you will gather knowledge step by step & easily run it on your server.

Contents
1. 2. 3. 4. What is SOAP? Why SOAP? Create web service method Consume web service

Background
What is SOAP?
SOAP is based on XML so it is considered human read. It is a protocol for accessing a Web Service. It is a simple XML-based protocol to let applications exchange information over HTTP.

Why SOAP?
In real field, so many applications are required for data communication between systems by Remote Procedure Calls (RPC) between objects like DCOM and CORBA but HTTP was not designed for this. RPC represents compatibility Security problem

Firewalls and proxy servers will normally block this kind of traffic. A better way to communicate between applications is over HTTP as HTTP is supported by all Internet browsers and servers. That's why it is preferable to SOAP Service. We can collaborate with other programmers building big size and complex applications in multiple platforms.

Using the Code


Here, I will create a web service using PHP code. So lets follow the steps given below: Step 1: You will download the library from http://sourceforge.net/projects/nusoap/. Step 2: Run WAMP server, then you will go to www root folder location. Step 3: Create folder, its called WebServiceSOAP into www root folder. Step 4: Paste "lib" folder inside your "www/WebServiceSOAP/ " location from Step 1 download files. Step 5: Create two files server.php and client.php into WebServiceSOAP folder location. Step 6: Inside server.php, please write the code lines given below:
< ? p h p / / c a l ll i b r a r y r e q u i r e _ o n c e( ' l i b / n u s o a p . p h p ' ) ; / / u s i n gs o a p _ s e r v e rt oc r e a t es e r v e ro b j e c t $ s e r v e r=n e ws o a p _ s e r v e r ; / / r e g i s t e raf u n c t i o nt h a tw o r k so ns e r v e r $ s e r v e r > r e g i s t e r ( ' g e t _ m e s s a g e ' ) ; / /c r e a t et h ef u n c t i o n f u n c t i o ng e t _ m e s s a g e ( $ y o u r _ n a m e ) { i f ( ! $ y o u r _ n a m e ) { r e t u r nn e ws o a p _ f a u l t ( ' C l i e n t ' , ' ' , ' P u tY o u rN a m e ! ' ) ; } $ r e s u l t=" W e l c o m et o" . $ y o u r _ n a m e. " .T h a n k sf o rY o u rF i r s tW e bS e r v i c eU s i n gP H Pw i t h S O A P " ; r e t u r n$ r e s u l t ; } / /c r e a t eH T T Pl i s t e n e r $ s e r v e r > s e r v i c e ( $ H T T P _ R A W _ P O S T _ D A T A ) ; e x i t ( ) ; ? >

Step 7: After creating server.php file, now we will test this server function. Please go to URL & type http://localhost/WebServiceSOAP/server.php?wsdl. Step 8: Create "client.php" file into WebServiceSOAP folder location. Please find the following code lines:
< ? p h p r e q u i r e _ o n c e( ' l i b / n u s o a p . p h p ' ) ; / / G i v ei tv a l u ea tp a r a m e t e r $ p a r a m=a r r a y (' y o u r _ n a m e '= >' M o n o t o s hR o y ' ) ; / / C r e a t eo b j e c tt h a tr e f e r e raw e bs e r v i c e s $ c l i e n t=n e ws o a p c l i e n t ( ' h t t p : / / l o c a l h o s t / W e b S e r v i c e S O A P / s e r v e r . p h p ' ) ; / / C a l laf u n c t i o na ts e r v e ra n ds e n dp a r a m e t e r st o o

$ r e s p o n s e=$ c l i e n t > c a l l ( ' g e t _ m e s s a g e ' , $ p a r a m ) ; / / P r o c e s sr e s u l t i f ( $ c l i e n t > f a u l t ) { e c h o" F A U L T :< p > C o d e :( " . $ c l i e n t > f a u l t c o d e . " < / p > " ; e c h o" S t r i n g :" . $ c l i e n t > f a u l t s t r i n g ; } e l s e { e c h o$ r e s p o n s e ; } ? >

Step 9: Save all codes, then please go to URL and type http://localhost/WebServiceSOAP/client.php.
R e s u l t :W e l c o m et oM o n o t o s hR o y .T h a n k sf o rY o u rF i r s tW e bS e r v i c eU s i n gP H Pw i t hS O A P

So, today I have shown you how to use simple SOAP web service and to get message from service at client end. Hope this post will help you to create a simple web service using SOAP. Post your queries if you have any or if there is anything else, please share. Keep visiting for new posts here. Have a good day!

History
21 st October, 2013: Initial post - Step by step understanding of web service functionality

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author


Monotosh Roy Mon
Architect Bangladesh

My Name is Monotosh Roy (Mon), Completed Bachelor In Computer Science from West Bengal University of Technology, India & M.Sc. In Computer Science & Engineering from United International University. I have lots of experience on core software development with API service integration more than 8 years using tools- ASP.Net, C#, PL/SQL, WCF, SAP, SugerCRM, Salesforce, VBA Excel Export, Web Service, PHP, and MVC 2/3/4 etc. I had successfully implemented more

You might also like