sdfsdnfslk 发表于 2015-8-24 09:25:30

PHP中soap的使用例子

  PHP 使用soap有两种方式。
  一、用wsdl文件
  服务器端。
<?php
class service
{
public function HelloWorld()
{
return&quot;Hello&quot;;
}
publicfunction Add($a,$b)
{
return $a+$b;
}
}
$server=new SoapServer('soap.wsdl',array('soap_version' => SOAP_1_2));
$server->setClass(&quot;service&quot;);
$server->handle();
?>
资源描述文件,可以用工具(zend studio)生成。其实就是一个xml文件。
  



<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<wsdl:definitions xmlns:soap=&quot;http://schemas.xmlsoap.org/wsdl/soap/&quot; xmlns:tns=&quot;http://localhost/interface/&quot; xmlns:wsdl=&quot;http://schemas.xmlsoap.org/wsdl/&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; name=&quot;soap&quot; targetNamespace=&quot;http://localhost/interface/&quot;>
<wsdl:types>
<xsd:schema targetNamespace=&quot;http://localhost/interface/&quot;>
<xsd:element name=&quot;HelloWorld&quot;>
<xsd:complexType>
<xsd:sequence>
<xsd:element name=&quot;in&quot; type=&quot;xsd:string&quot;/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name=&quot;HelloWorldResponse&quot;>
<xsd:complexType>
<xsd:sequence>
<xsd:element name=&quot;out&quot; type=&quot;xsd:string&quot;/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name=&quot;Add&quot;>
<xsd:complexType>
<xsd:sequence>
<xsd:element name=&quot;in&quot; type=&quot;xsd:int&quot;></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name=&quot;AddResponse&quot;>
<xsd:complexType>
<xsd:sequence>
<xsd:element name=&quot;out&quot; type=&quot;xsd:int&quot;></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name=&quot;AddRequest&quot;>   <wsdl:part name=&quot;a&quot; type=&quot;xsd:int&quot;></wsdl:part>
<wsdl:part name=&quot;b&quot; type=&quot;xsd:int&quot;></wsdl:part>
</wsdl:message>
<wsdl:message name=&quot;AddResponse&quot;>
<wsdl:part name=&quot;c&quot; type=&quot;xsd:int&quot;></wsdl:part>
</wsdl:message>
<wsdl:portType name=&quot;TestSoap&quot;>   <wsdl:operation name=&quot;Add&quot;>
<wsdl:input message=&quot;tns:AddRequest&quot;></wsdl:input>
<wsdl:output message=&quot;tns:AddResponse&quot;></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name=&quot;soapSOAP&quot; type=&quot;tns:TestSoap&quot;>
<soap:binding style=&quot;document&quot;
transport=&quot;http://schemas.xmlsoap.org/soap/http&quot; />
<wsdl:operation name=&quot;Add&quot;>
<soap:operation soapAction=&quot;http://localhost/interface/Add&quot; />
<wsdl:input>
<soap:body use=&quot;literal&quot;
namespace=&quot;http://localhost/interface/&quot; />
</wsdl:input>
<wsdl:output>
<soap:body use=&quot;literal&quot;
namespace=&quot;http://localhost/interface/&quot; />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name=&quot;TestSoap&quot;>
<wsdl:port binding=&quot;tns:soapSOAP&quot; name=&quot;soapSOAP&quot;>
<soap:address location=&quot;http://localhost/interface/myservice.php&quot;/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
客户端调用


<?php
$soap = new SoapClient('http://localhost/interface/soap.wsdl');
echo $soap->Add(1,2);
?>
  二、不用wsdl文件
  服务器端



<?php
class service
{
public function HelloWorld()
{
return&quot;Hello&quot;;
}
publicfunction Add($a,$b)
{
return $a+$b;
}
}
$server=new SoapServer(null,array('uri' => &quot;abcd&quot;));
$server->setClass(&quot;service&quot;);
$server->handle();
?>
客户端


<?php
try{
$soap = new SoapClient(null,array(
&quot;location&quot; => &quot;http://localhost/interface/soap.php&quot;,
&quot;uri&quot;      => &quot;abcd&quot;,//资源描述符服务器和客户端必须对应
&quot;style&quot;    => SOAP_RPC,
&quot;use&quot;      => SOAP_ENCODED
));
echo $soap->Add(1,2);
}catch(Exction $e){
echo print_r($e->getMessage(),true);
}
?>
页: [1]
查看完整版本: PHP中soap的使用例子