PDA

View Full Version : [PHP/XML] Webservices SOAP, struttura file wsdl


race2
08-05-2018, 12:44
Salve,
stò affrontando per la prima volta i web services SOAP in PHP, stò cercando di capire le basi e la struttura del file ".wsdl", ho trovato cosi tanti esempi e testi che non so a quale affidarmi, per ogni esempio la struttura cambia, i tag cambiano, insomma è tutto diverso...

Vi chiedo una mano per scegliere una guida dove imparare correttamente come realizzare un web services in PHP e SOAP, escludendo "nusoap e soap rest".

Se ci sono più modi di procedere vorrei imparare quello migliore a livello di sicurezza.

Grazie molte.

race2
08-05-2018, 16:14
Cosa c'è di sbagliato in questo wsdl ?? Funziona ma non credo assolutamente che sia scritto come si deve.


<?xml version="1.0" encoding="UTF-8"?>

<wsdl:definitions
name="Library"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="Library"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="Library"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

<xsd:documentation></xsd:documentation>

<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="Library">
<xsd:complexType name="my_value">
<xsd:sequence>
<xsd:element name="my_var" type="xsd:string"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>

<wsdl:message name="MyFunctionRequest">
<wsdl:part name="_request_" type="xsd:book"></wsdl:part>
</wsdl:message>
<wsdl:message name="MyFunctionResponse">
<wsdl:part name="_response_" type="tns:string"></wsdl:part>
</wsdl:message>

<wsdl:portType name="Library">
<wsdl:operation name="MyFunction_1">
<wsdl:input message="tns:MyFunctionRequest"/>
<wsdl:output message="tns:MyFunctionResponse"/>
</wsdl:operation>
<wsdl:operation name="MyFunction_2">
<wsdl:input message="tns:MyFunctionRequest"/>
<wsdl:output message="tns:MyFunctionResponse"/>
</wsdl:operation>
</wsdl:portType>

<wsdl:binding name="Library" type="tns:Library">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="MyFunction_1">
<soap:operation soapAction="http://127.0.0.1/ws_soap/server.php"/>
<wsdl:input>
<soap:body use="literal" namespace="Library"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal" namespace="Library"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="MyFunction_2">
<soap:operation soapAction="http://127.0.0.1/ws_soap/server.php"/>
<wsdl:input>
<soap:body use="literal" namespace="Library"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal" namespace="Library"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

<wsdl:service name="Library">
<wsdl:port binding="tns:Library" name="BookLibrary">
<soap:address location="http://127.0.0.1/ws_soap/server.php"/>
</wsdl:port>
</wsdl:service>

</wsdl:definitions>


Questo è il server:


ini_set('soap.wsdl_cache_enabled', 0);

class MyClass
{
public function MyFunction_1($my_value_1)
{
return $my_value_1;
}

public function MyFunction_2($my_value_2)
{
return $my_value_2;
}
}

$Service = new MyClass();

$server = new SoapServer("ws.wsdl");
$server->setObject($Service);
$server->handle();


Questo è il client:


ini_set('soap.wsdl_cache_enabled', 0);
ini_set('soap.wsdl_cache_ttl', 0);

$wsdl = 'http://127.0.0.1/ws_soap/server.php/method?WSDL';

$options = array(
'uri'=>'http://schemas.xmlsoap.org/soap/envelope/',
'style'=>SOAP_RPC,
'use'=>SOAP_ENCODED,
'soap_version'=>SOAP_1_1,
'cache_wsdl'=>WSDL_CACHE_NONE,
'connection_timeout'=>15,
'trace'=>true,
'encoding'=>'UTF-8',
'exceptions'=>true,
);

$objClient = new SoapClient($wsdl, $options);

try
{
if(isset($objClient))
{
$resp1 = $objClient->MyFunction_1("Ciao ");
$resp2 = $objClient->MyFunction_2(" sono Mario");

echo $resp1.$resp2;
}
}
catch(SoapFault $ex)
{
echo var_dump($ex);
}