PHP 使用soap有两种方式。
一、用wsdl文件
服务器端。
<?php
class service
{
public function HelloWorld()
{
return "Hello";
}
public function Add($a,$b)
{
return $a+$b;
}
}
$server=new SoapServer('soap.wsdl',array('soap_version' => SOAP_1_2));
$server->setClass("service");
$server->handle();
?>
资源描述文件,可以用工具(zend studio)生成。其实就是一个xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost/interface/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="soap" targetNamespace="http://localhost/interface/">
<wsdl:types>
<xsd:schema targetNamespace="http://localhost/interface/">
<xsd:element name="HelloWorld">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="in" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="HelloWorldResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="out" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Add">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="in" type="xsd:int"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="AddResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="out" type="xsd:int"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="AddRequest"> <wsdl:part name="a" type="xsd:int"></wsdl:part>
<wsdl:part name="b" type="xsd:int"></wsdl:part>
</wsdl:message>
<wsdl:message name="AddResponse">
<wsdl:part name="c" type="xsd:int"></wsdl:part>
</wsdl:message>
<wsdl:portType name="TestSoap"> <wsdl:operation name="Add">
<wsdl:input message="tns:AddRequest"></wsdl:input>
<wsdl:output message="tns:AddResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="soapSOAP" type="tns:TestSoap">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="Add">
<soap:operation soapAction="http://localhost/interface/Add" />
<wsdl:input>
<soap:body use="literal"
namespace="http://localhost/interface/" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal"
namespace="http://localhost/interface/" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="TestSoap">
<wsdl:port binding="tns:soapSOAP" name="soapSOAP">
<soap:address location="http://localhost/interface/myservice.php"/>
</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 "Hello";
}
public function Add($a,$b)
{
return $a+$b;
}
}
$server=new SoapServer(null,array('uri' => "abcd"));
$server->setClass("service");
$server->handle();
?>
客户端
<?php
try{
$soap = new SoapClient(null,array(
"location" => "http://localhost/interface/soap.php",
"uri" => "abcd", //资源描述符服务器和客户端必须对应
"style" => SOAP_RPC,
"use" => SOAP_ENCODED
));
echo $soap->Add(1,2);
}catch(Exction $e){
echo print_r($e->getMessage(),true);
}
?>
解析SOAPXML文件
$soap=<<<SOAP
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope 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:cwmp="urn:dslforum-org:cwmp-1-0">
<SOAP-ENV:Header>
<cwmp:ID SOAP-ENV:actor="http://schemas.xmlsoap.org/soap/actor/next" SOAP-ENV:mustUnderstand="1">1</cwmp:ID>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<cwmp:Inform>
<DeviceId xsi:type="DeviceldStruct">
<span style="white-space:pre"> </span><Manufacturer xsi:type="xsd:string">CYLIX</Manufacturer>
<span style="white-space:pre"> </span><OUI xsi:type="xsd:string">wmid</OUI>
<span style="white-space:pre"> </span><ProductClass xsi:type="xsd:string">CY-DZ1380</ProductClass>
<span style="white-space:pre"> </span><SerialNumber xsi:type="xsd:string">004201FF004188501442FCD5D901450H</SerialNumber>
<span style="white-space:pre"> </span></DeviceId>
<span style="white-space:pre"> </span><Event SOAP-ENC:arrayType="cwmp:EventStruct[1]">
<span style="white-space:pre"> </span><EventStruct>
<span style="white-space:pre"> </span><EventCode xsi:type="xsd:string">1 BOOT</EventCode>
<span style="white-space:pre"> </span><CommandKey xsi:type="xsd:string"></CommandKey>
<span style="white-space:pre"> </span></EventStruct>
<span style="white-space:pre"> </span></Event>
<span style="white-space:pre"> </span><MaxEnvelopes xsi:type="xsd:unsignedInt">1</MaxEnvelopes>
<span style="white-space:pre"> </span><CurrentTime xsi:type="xsd:dateTime">2014-12-01T09:21:01+08:00</CurrentTime>
<span style="white-space:pre"> </span><RetryCount xsi:type="xsd:unsignedInt">0</RetryCount>
<span style="white-space:pre"> </span><ParameterList SOAP-ENC:arrayType="cwmp:ParameterValueStruct[22]">
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.DeviceInfo.HardwareVersion</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">CY-DZ1380_1.0</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.DeviceInfo.SoftwareVersion</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">53.1.6</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.DeviceInfo.AdditionalHardwareVersion</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string"></Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.DeviceInfo.AdditionalSoftwareVersion</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string"></Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.ManagementServer.ConnectionRequestURL</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">http://192.168.1.109:7547</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.ManagementServer.ConnectionRequestUsername</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">admin</Value></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.ManagementServer.ConnectionRequestPassword</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">admin</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.DeviceInfo.ModelName</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">CY-DZ1380</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.DeviceInfo.Description</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">SDMC STB</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.DeviceInfo.FirstUseDate</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">2014-12-01 09:20:59</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.DeviceInfo.UpTime</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:int">09:20:58</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.Time.NTPServer1</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string"></Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.LAN.DNSServers</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">202.96.134.133</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.LAN.AddressingType</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">DHCP</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.LAN.IPAddress</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">192.168.1.109</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.LAN.MACAddress</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">FC:D5:D9:01:7F:89</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.X_CMCC_OTV.STBInfo.STBID</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">004201FF004188501442FCD5D901450H</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.X_CMCC_OTV.ServiceInfo.UserID</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">user</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.X_CMCC_OTV.ServiceInfo.PPPoEID</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string"></Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.X_CMCC_OTV.ServiceInfo.AuthURL</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">http://www.baidu.com</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.DeviceType</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">STB</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span><ParameterValueStruct>
<span style="white-space:pre"> </span><Name xsi:type="xsd:string">Device.DeviceSummary</Name>
<span style="white-space:pre"> </span><Value xsi:type="xsd:string">This is a Mbox</Value>
<span style="white-space:pre"> </span></ParameterValueStruct>
<span style="white-space:pre"> </span></ParameterList>
<span style="white-space:pre"> </span></cwmp:Inform>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOAP;
$clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:','cwmp:'], '', $soap);
$xml=simplexml_load_string($clean_xml);