Example of Calling a Web Service based on wsdl through PHP

 

There are two ways to call Web Service with PHP :

i) Calling Web Service with SOAP Client in PHP:
PHP comes up with an in built class that is SOAP Client. You can use this like below code
You just need to create an object of SOAP Client Class and it requires a wsdl URL as an arguement.
Below code snippet gives better explanation

<?php
$client = new SoapClient("http://site.info/index.php?/wpws/?wsdl");
echo "<pre />";
print_r($client->getPosts(101));
echo "</pre>";
?>

This calling is based on wsdl file. Here getPost is a method that is created on remote server that is being consumed by all clients.

ii) We have external library available here(http://sourceforge.net/projects/nusoap/).

<?php

// Use the NuSOAP php library
require_once('lib/nusoap.php');

// Set parameters
$userName = 'usertest';
$password = 'testing';

// Build SOAP message. The objective of below code is to pass username and password into header into webservice for Login. Then it calls the testLogin
$body = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<UserInfo xmlns="http://yournameserverurl">
<UserName>'.$userName.'</UserName>
<Password>'.$password.'</Password>
</UserInfo>
</soap:Header>
<soap:Body>
<testLogin xmlns="http://yournameserverurl" />
</soap:Body>
</soap:Envelope>';

// Set up web service path and SOAP action variable
$serverpath = "http://yourserverpath";
$soapaction = "http://abc.com/testLogin";

// Create new SOAP client object instance
$client = new nusoap_client($serverpath);

$results = $client->send($body, $soapaction,'');

// Display output
echo "<h2>Request</h2>";
echo "<pre>" . htmlspecialchars($client->request,ENT_QUOTES) . "</pre>";
echo "<h2>Response</h2>";
echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>";

// Dispose SOAP client
unset($client);

?>

Related posts:

The following two tabs change content below.

Chandra Shekhar

Consultant at Infogain
Chandra Shekhar is a LAMP Developer,Thinker,Blogger and currently working as Magento(Ecommerce) specialist.. I am always open to learn new things in LAMP as well as other newer technology. In the journey of my last 9 years of web and product development, I have decided to share my knowledge and experience to Open Source Community in 2011. In this regard I have started CuteHits.com. Since then I am actively sharing my knowledge on various LAMP technology through CuteHits.com’s technical ... Read More about Chandra Shekhar

Latest posts by Chandra Shekhar (see all)