php soap webservice 实例
step1:修改php.ini
extension=php_soap.dll
step2:服务器端代码
soapserver.php
class service { private $username = ""; private $password = ""; function __construct() { $xml = file_get_contents('php://input'); $p = xml_parser_create(); xml_parse_into_struct($p, $xml, $vals, $index); xml_parser_free($p); $this->username = $vals[5]['value']; $this->password = $vals[9]['value']; $logs .= date("Y-m-d H:i:s")."\r\n"; $logs.=$xml.="\r\n"; $logs .= "username:".$this->username."\r\n" ; $logs .= "password:".$this->password."\r\n"; //写日志 $path = "./log".date("YmdH").".txt"; if(!is_writable($path)){ @touch($path); } $fp = @fopen($path,'a'); if($fp){ @fwrite($fp,$logs); } $this->isLogin(); } public function isLogin() { if($this->username != '1' || $this->password != md5('1')){ throw new SoapFault('1001', '您无权访问'); } } public function Add($a,$b) { return $a+$b; } public function Sub($a,$b) { return $a-$b; } public function Say($name){ return " Hello ".$name; } } $server=new SoapServer('http://localhost/test/soap/soap.wsdl',array('soap_version' => SOAP_1_2,'actor' => 'laruence')); $server->setClass("service"); $server->handle();
3、客户端代码
client.php
$soap = new SoapClient('http://localhost/test/soap/soap.wsdl',array("trace"=>true)); $header = new SoapHeader('http://localhost/test/soap/', 'auth', array("username"=>"1","password"=>md5("1")), false, SOAP_ACTOR_NEXT); $soap->__setSoapHeaders(array($header)); try { echo $pack = $soap->Say("123"); echo "\r\n"; echo $pack = $soap->Add(123,456); } catch (Exception $e) { echo $soap->__getLastRequest(); echo $soap->__getLastResponse(); echo $e->getMessage(); }
4、WSDL文件,可以用zend Studio 生成
<?xml version="1.0" encoding="UTF-8" ?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost/test/soap/soapserver.php" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="soap" targetNamespace="http://localhost/test/soap/"> <!-- 定义数据类型 --> <wsdl:types> <xsd:schema targetNamespace="http://localhost/test/soap/"> <xsd:element name="Add"> <xsd:complexType> <xsd:sequence> <xsd:element name="in" type="xsd:int" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="AddResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="out" type="xsd:int" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> <xsd:schema targetNamespace="http://localhost/test/soap/"> <xsd:element name="Sub"> <xsd:complexType> <xsd:sequence> <xsd:element name="in" type="xsd:int" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="SubResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="out" type="xsd:int" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> <xsd:schema targetNamespace="http://localhost/test/soap/"> <xsd:element name="Say"> <xsd:complexType> <xsd:sequence> <xsd:element name="in" type="xsd:sting" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="SayResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="out" type="xsd:string" /> </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:message name="SubRequest"> <wsdl:part name="d" type="xsd:int"> </wsdl:part> <wsdl:part name="e" type="xsd:int"> </wsdl:part> </wsdl:message> <wsdl:message name="SubResponse"> <wsdl:part name="f" type="xsd:int"> </wsdl:part> </wsdl:message> <wsdl:message name="SayRequest"> <wsdl:part name="g" type="xsd:string"> </wsdl:part> </wsdl:message> <wsdl:message name="SayResponse"> <wsdl:part name="h" type="xsd:string"> </wsdl:part> </wsdl:message> <!-- 方法 --> <wsdl:portType name="soap"> <wsdl:operation name="Add"> <wsdl:input message="tns:AddRequest" /> <wsdl:output message="tns:AddResponse" /> </wsdl:operation> <wsdl:operation name="Sub"> <wsdl:input message="tns:SubRequest" /> <wsdl:output message="tns:SubResponse" /> </wsdl:operation> <wsdl:operation name="Say"> <wsdl:input message="tns:SayRequest" /> <wsdl:output message="tns:SayResponse" /> </wsdl:operation> </wsdl:portType> <wsdl:binding name="soapSOAP" type="tns:soap"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> <wsdl:operation name="Add"> <soap:operation soapAction="http://localhost/test/soap/Add" /> <wsdl:input> <soap:body use="literal" namespace="http://localhost/test/soap/" /> </wsdl:input> <wsdl:output> <soap:body use="literal" namespace="http://localhost/test/soap/" /> </wsdl:output> </wsdl:operation> <wsdl:operation name="Sub"> <soap:operation soapAction="http://localhost/test/soap/Sub" /> <wsdl:input> <soap:body use="literal" namespace="http://localhost/test/soap/" /> </wsdl:input> <wsdl:output> <soap:body use="literal" namespace="http://localhost/test/soap/" /> </wsdl:output> </wsdl:operation> <wsdl:operation name="Say"> <soap:operation soapAction="http://localhost/test/soap/Sub" /> <wsdl:input> <soap:body use="literal" namespace="http://localhost/test/soap/" /> </wsdl:input> <wsdl:output> <soap:body use="literal" namespace="http://localhost/test/soap/" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="soap"> <wsdl:port binding="tns:soapSOAP" name="soapSOAP"> <soap:address location="http://localhost/test/soap/soapserver.php" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
现主要从事PHP、Uinx/Linux、C/C++等方面的项目开发。