PHP SOAP and WSDL | Lessoncup Programming Blog

Monday, 23 December 2013

PHP SOAP and WSDL

Hi, today i am going to tell you about how to create a SOAP Object with PHP, before that we must know about what is SOAP, a SOAP is ( SIMPLE OBJECT ACCESS PROTOCOL ) which is used for creating a Web Service, the advantage of SOAP is we can communicate with multiple and different operating system and work with multiple programming platforms on HTTP request, we can write the SOAP in XML based language, let's see how to create a simple soap server and soap client using php


What is SOAP SERVER ?

SOAP SERVER
is a Constructor and provide a server to access the WSDL file and you can use this with WSDL or without WSDL service discretion, you can create the soap server in php using new keyword see this code new SoapServer("some.wsdl");

What is SOAP CLIENT ?

SOAP CLIENT
 is a Constructor and provide a client to access the WSDL file and you can use this with WSDL or without WSDL service discretion, you can create the soap client in php using new keyword see this code new SoapClient ("some.wsdl"); 

What is WSDL ?

WSDL is Web service Description Language and it is written in XML based language to describe the web services, it is a place where we write the php function names to access the SoapServer and in same wsdl file we also use the php file name with that wsdl file access the given function from php file, see the bellow XML code which i have wrote to access the users data from database
<?xml version ='1.0' encoding ='UTF-8' ?>
<definitions name='Users' 
  targetNamespace='http://demos.pixelmax.in/soapandwsdl/users.wsdl' 
  xmlns:tns='http://demos.pixelmax.in/soapandwsdl/users.wsdl' 
  xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' 
  xmlns='http://schemas.xmlsoap.org/wsdl/'> 

<message name='getUsersRequest'> 
  <part name='symbol' type='xsd:string'/>
</message> 
<message name='getUsersResponse'> 
  <part name='Result' type='xsd:string'/> 
</message> 


<portType name='UsersPortType'> 
  <operation name='getUsers'>
    <input message='tns:getUsersRequest'/> 
    <output message='tns:getUsersResponse'/>   
  </operation> 
</portType> 



<binding name='UsersBinding' type='tns:UsersPortType'> 
  <soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http'/> 
  <operation name='getUsers'> 
    <soap:operation soapAction='urn:localhost'/> 
    <input> 
      <soap:body use='encoded' namespace='urn:localhost' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
    </input> 
    <output> 
      <soap:body use='encoded' namespace='urn:localhost' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
    </output> 
  </operation>      
</binding> 

<service name='UsersService'> 
  <port name='UsersPort' binding='UsersBinding'> 
    <soap:address location='http://demos.pixelmax.in/soapandwsdl/users.php'/> 
  </port> 
</service>
</definitions> 

users.php (Creating Soap Server)

in this file create the function and write the query to fetch the users data from database and using json_encode() method convert the php array to json object,  remind when ever you work with soap and wsdl you must set the cache of wsdl in server using ini_set("soap.wsdl_cache_enabled","0");

now create the Soap Server using new SoapServer('users.wsdl') and keep the wsdl file inside the constructor now we need to provide the function name to Soap Client with that the client access the data, use addFunction("getUsers") method it is predefined function of SOAP Class and inside the function assign the php function name like getUsers, now use handle() method to handle the SOAP request
<?php

include('db.php');

function getUsers(){

   $ss= mysql_query("select * from users");

   $data = array();
   
   while ($row=mysql_fetch_array($ss)){
   
   $data[] = $row['uid'] . " " . $row['username'] . " " . $row['email'] . " " . $row['gender'].'<br/>';

   $fabdata = json_encode($data);

   }
   
   return $fabdata;
 
}

ini_set("soap.wsdl_cache_enabled","0");
$server = new SoapServer("users.wsdl");
$server->AddFunction("getUsers");
$server->handle();

?>

index.php (Creating Soap Client)

here first turn off the wsdl cache, now create the soap client using new SoapClient("users.wsdl") and give the wsdl file inside the function now call the function name which is written in users.wsdl file like getUsers() now decode the JSON encoded data which is coming from function using json_decode() method, now just print or loop the elements you can able to see the result..
<?php
// turn off the WSDL cache
ini_set ("soap.wsdl_cache_enabled", "0");

$client = new SoapClient("users.wsdl");

$us = $client->getUsers();

$users= (array) json_decode($us);

while (list($key, $val) = each($users)) {

echo "$key => $val\n";

}

?>

Download Script

5 comments :

  1. can i access getUsers method in android App environment.
    Let me know if you know how to use it with cross platform

    ReplyDelete
  2. is getUsers is default function of wsdl ?
    how can i declare custom function with this environment?

    ReplyDelete
    Replies
    1. ok i got it. Thanks for initiating me to learn webservice with SOAP and PHP.
      Thanks a lot.
      but i didn't got my first question answer.plz let me know how to use it with android.

      Delete
  3. confusing tutorial but gud effort :)
    try to elaborate more

    ReplyDelete

Mohammad Khasim
Blogger | Web Developer
Hyderabad - INDIA
lessoncup@gmail.com

PHP | My SQL | JQUERY
Web Development Tools | CMS | Frameworks | SEO

Get my Latest Lessons