Создание сервиса на основе готового wsdl

Fedak.o.b

Новичок
Добрый день!

Нужно создать Soap сервис.

Долгими усилиями создал wsdl - файл, так как сгенерировать с помощью NuSoap так и не получилось:
PHP:
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions 
	xmlns:s="http://www.w3.org/2001/XMLSchema" 
	xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" 
	xmlns:tns="http://site.com/" 
	xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
	xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" 
	xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" 
	xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
	targetNamespace="http://site.com/" 
	xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://site.com/">
      <s:element name="GetPrice">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="PartNr" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="GetPriceResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="GetPriceResult" type="tns:ArrayOfPricePosObj" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:complexType name="ArrayOfPricePosObj">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="unbounded" name="PricePosObj" nillable="true" type="tns:PricePosObj" />
        </s:sequence>
      </s:complexType>
      <s:complexType name="PricePosObj">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="1" name="PartNr" type="s:string" />
          <s:element minOccurs="0" maxOccurs="1" name="PartName" type="s:string" />
          <s:element minOccurs="1" maxOccurs="1" name="PriceClient" type="s:double" />
        </s:sequence>
      </s:complexType>
    </s:schema>
  </wsdl:types>
  
  <wsdl:message name="GetPriceSoapIn">
    <wsdl:part name="parameters" element="tns:GetPrice" />
  </wsdl:message>
  <wsdl:message name="GetPriceSoapOut">
    <wsdl:part name="parameters" element="tns:GetPriceResponse" />
  </wsdl:message>
  
  <wsdl:portType name="PricesSoap">
    <wsdl:operation name="GetPrice">
      <wsdl:input message="tns:GetPriceSoapIn" />
      <wsdl:output message="tns:GetPriceSoapOut" />
    </wsdl:operation>
  </wsdl:portType>
  
  <wsdl:binding name="PricesSoap" type="tns:PricesSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="GetPrice">
      <soap:operation soapAction="http://site.com/GetPrice" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  
  <wsdl:service name="Prices">
    <wsdl:port name="PricesSoap" binding="tns:PricesSoap">
      <soap:address location="http://site.com/server.php" />
    </wsdl:port>
  </wsdl:service>
  
</wsdl:definitions>
1. Хотелось бы узнать верно ли созданный wsdl для дальнейшего получения данных с бд:
- PartNr
- PartName
- PriceClient

2. Вашы рекомендации по написанию SOAP сервиса.

Прочитал:
http://phpclub.ru/detail/article/soap
http://www.w3.org/TR/wsdl
http://www.php.ru/forum/viewtopic.php?f=13&t=25393
 

grigori

( ͡° ͜ʖ ͡°)
Команда форума
soap на php - это действительно неординарная задача

> 2. Вашы рекомендации по написанию SOAP сервиса.

сделать json-rpc, протокол уже очень популярен
 

Fedak.o.b

Новичок
Прочитал http://www.tutorialspoint.com/wsdl/index.htm (с данного материала вынес много полезного)

exemple.wsdl
PHP:
<?xml version="1.0" encoding="utf-8"?>
<definitions name="PricesService"
	targetNamespace="http://wsdl.toplevel/soap2/exemple.wsdl"
	xmlns="http://schemas.xmlsoap.org/wsdl/"
	xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://wsdl.toplevel/soap2/exemple.wsdl"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
	xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> 

	<types>
		<schema elementFormDefault="qualified" targetNamespace="http://wsdl.toplevel/soap2/exemple.wsdl" xmlns="http://www.w3.org/2000/10/XMLSchema">
			<element name="PricesRequest">
				<complexType>
					<sequence>
						<element name="PartNr" type="string" />
					</sequence>
				</complexType>
			</element>
			
			<element name="PricesResponse">
				<complexType>
					<sequence>
						<element name="PartNr" type="string" />
						<element name="PartName" type="string" />
						<element name="PriceClient" type="string" />
					</sequence>
				</complexType>
			</element>
		</schema>
	</types>	
		
	<message name="PricesSoapRequest">
		<part name="parameters" type="xsd:string" />
	</message>
	<message name="PricesSoapResponse">
		<part name="parameters" type="xsd:string" />
	</message>
  
	<portType name="PricesPortType">
		<operation name="getPrices">
			<input message="tns:PricesSoapRequest" />
			<output message="tns:PricesSoapResponse" />
		</operation>
	</portType>
  
	<binding name="PricesBinding" type="tns:PricesPortType">
		<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
		<operation name="getPrices">
			<soap:operation soapAction="getPrices"/>
			<input>
				<soap:body
				encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
				namespace="urn:examples:helloservice"
				use="encoded"/>
			</input>
			<output>
				<soap:body
				encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
				namespace="urn:examples:helloservice"
				use="encoded"/>
			</output>
		</operation>
	</binding>
  
	<service name="PricesService">
		<documentation>WSDL File for HelloService</documentation>
		<port binding="tns:PricesBinding" name="PricesPort">
			<soap:address location="http://wsdl.toplevel/soap2/server.php" />
		</port>
	</service>
  
</definitions>
server.php
PHP:
<?php 
class QuoteService
{
	// Good					
	//private $price = array("11" => "tester1", "22" => "tester2");
	
	//
	private $price = array(
							"11" => 
								array("PartNr" => "11", "PartName" => "Toster", "PriceClient" => "102.45"),
							"22" => 
								array("PartNr" => "22", "PartName" => "Toster22", "PriceClient" => "999.99")
							);

	function getPrices($parameters)
	{
		if (isset($this->price[$parameters]))
		{ 
			return $this->price[$parameters]; 
		}
		else
		{ 
			throw new SoapFault("Server","Unknown set_partnr '".$parameters."'."); 
		} 
	} 
} 

ini_set("soap.wsdl_cache_enabled", "0");
$server = new SoapServer("exemple.wsdl"); 
$server->setClass("QuoteService"); 
$server->handle(); 
?>
client.php
PHP:
$client = new SoapClient("exemple.wsdl", array( 
	"trace"      => 1, 
	"exceptions" => 0));

//$price_item = $client->getPrices("11");

$price_item = $client->__call("getPrices", array("PartNr" => "11"));

print "\n\n\n\n\n\n\n<pre>"; 
	print "Запрос :\n".htmlspecialchars($client->__getLastRequest());
	print $client->__getLastRequest()."\n\n\n\n";
	print "Ответ:\n".htmlspecialchars($client->__getLastResponse()); 
	print $client->__getLastResponse();
print "</pre>\n\n\n";

Получается когда server.php содержит:
PHP:
private $price = array("11" => "tester1", "11" => "tester2");
И с client.php
передаю: "PartNr" => "11",
в ответ получаю: tester1.


А кода server.php содержит:
PHP:
private $price = array(
							"11" => 
								array("PartNr" => "11", "PartName" => "Toster", "PriceClient" => "102.45"),
							"22" => 
								array("PartNr" => "22", "PartName" => "Toster22", "PriceClient" => "999.99")
							);
И с client.php
передаю: "PartNr" => "11",
в ответ получаю: Array.
Обработал переменную print_r, но результат остался тот же ;(

Вопрос. Как же получить массив элементов?
 

fixxxer

К.О.
Партнер клуба
Вроде ты и клиент и сервер сам пишешь? Почему тогда SOAP? Мне соап всегда казался типичным примером оверинжиниринга: наворочено мильон сущностей на совершенно пустом месте. Советую REST + JSON.
 

Fedak.o.b

Новичок
Вроде ты и клиент и сервер сам пишешь?
Задача написать сервер с которым смогут работать клиєнти!
Но клиенской части сразу тестирую, потом же будет нужен мануал для роботы с сервисом.

Почему тогда SOAP? Мне соап всегда казался типичным примером оверинжиниринга: наворочено мильон сущностей на совершенно пустом месте. Советую REST + JSON.
Возьму себе на будущие :)
Но сейчас все же хотелось бы доделать данную задачу на soap-пе! )
 
Сверху