Click here to Skip to main content
15,881,856 members
Articles / Programming Languages / PHP
Tip/Trick

Creating Web Service Using PHP Within 10 Minutes

Rate me:
Please Sign up or sign in to vote.
4.42/5 (29 votes)
30 Oct 2013CPOL2 min read 552K   11.8K   32   62
Developing web service using PHP within 10 minutes

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. What is SOAP?
  2. Why SOAP?
  3. Create web service method
  4. 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 let’s 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, it’s 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:

PHP
<?php
//call library 
require_once ('lib/nusoap.php'); 
//using soap_server to create server object 
$server = new soap_server; 

//register a function that works on server 
$server->register('get_message'); 

// create the function 
function get_message($your_name) 
{ 
if(!$your_name){ 
return new soap_fault('Client','','Put Your Name!'); 
} 
$result = "Welcome to ".$your_name .". Thanks for Your First Web Service Using PHP with SOAP"; 
return $result; 
} 
// create HTTP listener 
$server->service($HTTP_RAW_POST_DATA); 
exit(); 
?>  

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:

PHP
<?php 
require_once ('lib/nusoap.php'); 
//Give it value at parameter 
$param = array( 'your_name' => 'Monotosh Roy'); 
//Create object that referer a web services 
$client = new soapclient('http://localhost/WebServiceSOAP/server.php'); 
//Call a function at server and send parameters too 
$response = $client->call('get_message',$param); 
//Process result 
if($client->fault) 
{ 
echo "FAULT: <p>Code: (".$client->faultcode."</p>"; 
echo "String: ".$client->faultstring; 
} 
else 
{ 
echo $response; 
} 
?> 

Step 9: Save all codes, then please go to URL and type http://localhost/WebServiceSOAP/client.php.

Result: Welcome to Monotosh Roy. Thanks for Your First Web Service Using PHP with SOAP

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

  • 21st 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)


Written By
Architect
Bangladesh 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 than 200 projects in boundary as well across boundary followed by project management tools like as Agile, PMP & BCM-

Worked on freelancing marketplace in US & other countries through Odesk, Elance & Freelancer etc

LinkedIn Profile- bd.linkedin.com/in/monotoshroy/

Comments and Discussions

 
Questionnot working on xamp Pin
vivekharnal13-Nov-14 20:19
vivekharnal13-Nov-14 20:19 
AnswerRe: not working on xamp Pin
Member 1175569710-Jun-15 1:18
Member 1175569710-Jun-15 1:18 
GeneralMy vote of 1 Pin
arpit goyal13811-Nov-14 19:58
arpit goyal13811-Nov-14 19:58 
GeneralRe: My vote of 1 Pin
Iqbal Habibie12-Mar-15 15:36
Iqbal Habibie12-Mar-15 15:36 
QuestionError Pin
Member 1102096820-Aug-14 19:28
Member 1102096820-Aug-14 19:28 
QuestionUncaught SoapFault exception Pin
JoshuaAdrielF11-Aug-14 21:25
JoshuaAdrielF11-Aug-14 21:25 
GeneralMy vote of 1 Pin
CodingLover22-Jul-14 18:51
CodingLover22-Jul-14 18:51 
QuestionHow to get XML File ? Pin
Member 105872342-Jul-14 9:45
Member 105872342-Jul-14 9:45 
Hello sir,
Thanks for article.
I have one query, i want to know how to make or generate XML File after using web service..it means result of service must be generated in XML file, so next time just send xml file in any platform..
In short (i am android developer) i want to use web service and fetch result of web service through XML, and use those stuff in my app.

How to do that...

Example 1:
Suppose make calculator app in android, code of calculator app distribute to other platform like web application, other mobile Application. so next time i have to not create code for each platform and i want to use all functions are created in web service and access method which is define in web service...

Example 2:
Mysql database data are use in android app, is create all functionality created like CRUID Operation define in web service? you understand what i am saying... MySQL Data -> android app,, through web service... how? (XML / JSON parsing..)

in short where is XML file?

Thanks in Advance..
Questioncode changed on nusoap Pin
ali yeganeh5-Jun-14 23:48
ali yeganeh5-Jun-14 23:48 
Questionim use xampp 1.8.3 and i have one error Pin
phungvanthuy260119884-Jun-14 11:50
phungvanthuy260119884-Jun-14 11:50 
GeneralMy vote of 1 Pin
Member 1082418916-May-14 9:49
Member 1082418916-May-14 9:49 
QuestionError Pin
Rameshwar Solanki28-Apr-14 23:17
Rameshwar Solanki28-Apr-14 23:17 
AnswerRe: Error Pin
atrick24-May-14 2:04
atrick24-May-14 2:04 
AnswerRe: Error Pin
Member 111250021-Oct-14 23:45
Member 111250021-Oct-14 23:45 
QuestionA little modification Pin
DevMostafa24-Mar-14 5:52
DevMostafa24-Mar-14 5:52 
Questionservice not provide WSDL Pin
Member 106464855-Mar-14 12:30
Member 106464855-Mar-14 12:30 
AnswerRe: service not provide WSDL Pin
Member 111250021-Oct-14 23:42
Member 111250021-Oct-14 23:42 
QuestionWSDL PinPopular
amjad farazi26-Feb-14 22:59
amjad farazi26-Feb-14 22:59 
AnswerRe: WSDL Pin
CodingLover22-Jul-14 18:42
CodingLover22-Jul-14 18:42 
GeneralRe: WSDL Pin
Member 111250021-Oct-14 23:43
Member 111250021-Oct-14 23:43 
QuestionNo answer : white page ! Pin
pierrix2520-Feb-14 8:01
pierrix2520-Feb-14 8:01 
QuestionNo answer : white page ! Pin
pierrix258-Feb-14 5:18
pierrix258-Feb-14 5:18 
AnswerRe: No answer : white page ! Pin
pierrix2512-Feb-14 11:21
pierrix2512-Feb-14 11:21 
QuestionError Pin
nima.sh3-Feb-14 15:59
nima.sh3-Feb-14 15:59 
AnswerRe: Error Pin
Srihari Sahu12-Feb-14 20:28
Srihari Sahu12-Feb-14 20:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.