Практические примеры по протоколу
SOAPПример 1.
Клиент
<?php
echo "<h1>ПРОТОКОЛ SOAP<br></h1><hr>";
@$sub=$_GET['sub'];
@$rate=$_GET['rate'];
if (!$sub)
{
echo "<h1>Введите цену и ставку налога</h1>
<form>
<p>Цена:<input type=text name='sub'>
Ставка налога:<input type=text name='rate'>
<input type=submit><input type=reset>
</form>";
}
else
{
$parameters=array('rate'=>$_GET['rate'],'sub'=>$_GET['sub']);
require_once('nusoap.php');
$sclient=new soapclient('http://localhost/NUSOAP/newserver1.php');
$result=$sclient->call('calc',$parameters);
echo 'РЕЗУЛЬТАТ РАСЧЕТА: ',$result;
echo '<h2>Запрос</h2>';
echo '<pre>'. htmlspecialchars($sclient->request, ENT_QUOTES).'</pre>';
echo '<h2>Ответ</h2>';
echo '<pre>'. htmlspecialchars($sclient->response, ENT_QUOTES).'</pre>';
}
?>
Инициализация параметров
Сервер
<?php
require_once('nusoap.php');
$server = new soap_server;
$server->register('calc');
function calc($rate,$sub)
{
$result=$sub*$rate/100.+$sub;
return $result;
}
$HTTP_RAW_POST_DATA=isset($HTTP_RAW_POST_DATA)? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
Результат
Пример 2 с включением
WSDL.Клиент
<?php
// Подключаем код NuSOAP
//(библиотека РНР упрощает создание клиентов и серверов SOAP)
require_once('nusoap.php');
// Создаем экземпляр клиента
$client = new soapclient('http://localhost/NUSOAP/server_wdsl.php?wsdl', true);
// Вызываем SOAP-метод
$result = $client->call('hello', array('name' => 'Saule'));
// Отображаем результат
echo '<h2>Результат</h2><pre>';
print_r($result);
echo '</pre>';
// Отображаем запрос и ответ
echo '<h2>Запрос</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Ответ</h2>'; echo '<h2>Ответ</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
// Отображаем отладочные сообщения
//echo '<h2>Отладка</h2>';
//echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>
Сервер
<?php
// Подключаем код NuSOAP
//(библиотека РНР упрощает создание клиентов и серверов SOAP)
require_once('nusoap.php');
$server = new soap_server();
// Создаем экземпляр сервера// Инициализируем поддержку WSDL
//$server->configureWSDL('hellowsdl', 'urn:hellowsdl');
$server->configureWSDL('server_wsdl.php', 'urn:server_wsdl');
// Устанавливаем пространство имен с префиксом tns для WSDL-схемы
$server->wsdl->schemaTargetNamespace = 'urn:server_wsdl';
// Регистрируем предоставляемый метод
$server->register('hello', // название метода
array('name' => 'xsd:string'), // входные параметры
array('return' => 'xsd:string'), // выходные параметры
'urn:server_wsdl', // пространство имен
'urn:server_wsdl#hello', // soapaction
'rpc', // стиль
'encoded', // использование
'Says hello to the caller' ); // описание
// Определяем метод как функцию PHP
function hello($name) {
return 'Salem, ' . $name;}
// Используем HTTP-запрос чтобы вызвать сервис
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ?
$HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA); ?>
Результат
Salem,Saule
____________________________________
Запрос
POST /NUSOAP/server_wdsl.php HTTP/1.0
Host: localhost
User-Agent: NuSOAP/0.9.5 (1.123)
Content-Type: text/xml; charset=ISO-8859-1
SOAPAction: "urn:server_wsdl#hello"
Content-Length: 516
<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:server_wsdl">
<SOAP-ENV:Body><tns:hello xmlns:tns="urn:server_wsdl">
<name xsi:type="xsd:string">Saule</name></tns:hello></SOAP-ENV:Body>
</SOAP-ENV:Envelope>
HTTP/1.1 200 OK
Date: Mon, 21 Feb 2011 08:31:42 GMT
Server: Apache/2.0.53 (Win32)
X-Powered-By: PHP/5.0.4
X-SOAP-Server: NuSOAP/0.9.5 (1.123)
Content-Length: 515
Connection: close
Content-Type: text/xml; charset=ISO-8859-1
<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body><ns1:helloResponse xmlns:ns1="urn:server_wsdl">
<return xsi:type="xsd:string">Salem, Saule</return></ns1:helloResponse>
</SOAP-ENV:Body></SOAP-ENV:Envelope>
WSDL примера 2
<?xml version="1.0" encoding="ISO-8859-1" ?>
<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:server_wsdl" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="urn:server_wsdl">
<types>
<xsd:schema targetNamespace="urn:server_wsdl">
<xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
<xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
</xsd:schema>
</types>
<message name="helloRequest">
<part name="name" type="xsd:string" />
</message>
<message name="helloResponse">
<part name="return" type="xsd:string" />
</message>
<portType name="server_wsdl.phpPortType">
<operation name="hello">
<documentation>Says hello to the caller</documentation>
<input message="tns:helloRequest" />
<output message="tns:helloResponse" />
</operation>
</portType>
<binding name="server_wsdl.phpBinding" type="tns:server_wsdl.phpPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="hello">
<soap:operation soapAction="urn:server_wsdl#hello" style="rpc" />
<input>
<soap:body use="encoded" namespace="urn:server_wsdl" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="urn:server_wsdl" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>
<service name="server_wsdl.php">
<port name="server_wsdl.phpPort" binding="tns:server_wsdl.phpBinding">
<soap:address location="http://localhost/NUSOAP/server_wdsl.php" />
</port>
</service>
</definitions>