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

21 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
  4. Such developers with solid fundamental information and experience acknowledge a wide range of challenges and targets profiting the purchasers and no more. Top Expert Cakephp Developers

    ReplyDelete
  5. Arranged in a straightforward manner, PHP gives more versatility than C, C++ and ASP and general causes in growing development to the site. It has a clear and viably reasonable semantic structure.Why use Laravel

    ReplyDelete
  6. The first is about the instrument of patent itself and whether the way of security it gives is fit to the software business. The second is the idea of software and whether it ought to be liable to protecting. itools 4 crack license key free download

    ReplyDelete
  7. A backlink is a link created when one website links to another. Backlinks are important to SEO & impact for higher ranking. In my 7+ years seo Career i see, without backlinks a website doesn't rank higher on google SERP.

    Get Your 300+ High Quality DoFollow Backlinks Here!

    Order Now with Full Confidence & 100% satisfaction.

    ReplyDelete
  8. With Brandsoo, it’s never been easier or more convenient to shop high quality domain names and professional logos brands for sale that’ll instantly give your brand a leg up and resonate with your audience! With many different domain sellers all competing for your business, you need to know where your business and brand will see.

    ReplyDelete
  9. Forex Signals, MT4 and MT5 Indicators, Strategies, Expert Advisors, Forex News, Technical Analysis and Trade Updates in the FOREX IN WORLD

    Forex Signals Forex Strategies Forex Indicators Forex News Forex World

    ReplyDelete
  10. Forex Signals, MT4 and MT5 Indicators, Strategies, Expert Advisors, Forex News, Technical Analysis and Trade Updates in the FOREX IN WORLD

    Forex Signals Forex Strategies Forex Indicators Forex News Forex World

    ReplyDelete
  11. Situs judi bola online UFA88 Terpercaya, juga memiliki casino online seperti, baccarat, roulette, Judi slot online, sabung ayam dan dadu online.

    ReplyDelete
  12. Adam the alien - Animated films A close encounter with a likable robot from another solar system leads to a star-crossed friendship Do aliens exist and an impossible intergalactic mission to save Extraterrestrial life the robot’s friends— despite overwhelming New cartoon movies odds.

    ReplyDelete
  13. TogelResult.info - Adalah Situs data hk Khusus yang menyediakan Daftar keluaran hk Result Nomor Togel dan Data hasil keluaran live draw hk seperti hk sgp sydney yang sudah terpercaya keluaran togel hk.

    ReplyDelete
  14. Shield Security Solutions Provides Ontario Security Training, Security Guard License or Security License in Ontario. Get Started Today

    ReplyDelete
  15. Vince Offer is and actor, producer and infomercial star. He is the best known infomercial star in the world. His products the SHAMWOW, InVINCEable, SlapChop, Schticky and more have sold millions. Vince Offer has also produced and starred in the films Underground Comedy Movie and The Inappropriate Comedy with Adrian Brody, Dante and Michelle Rodriguez.

    ReplyDelete
  16. Website laten maken? Onze professionals bouwen een omzetverhogende, professionele én unieke website of webshop! Een website laten maken kan vandaag nog

    Website laten maken | webdesigner | website freelancer | webdeveloper | webdesigner

    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