php编写最简单的webservice
先定义一个类文件 test.class.php
<?php class CTest { public function __construct() { } /** * * @param string $oParams * @return string */ public function Add($oParams) { $sParams = $oParams->oParams; $oParams = json_decode($sParams); $a = $oParams->a; $b = $oParams->b; $c =$a+$b; return array('ccc'=>$c); } } ?>
定义一个testServer.php作为WebService
<?php //error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE); include_once('./test.class.php'); $server = new SoapServer('wscache/CTest.1.0.wsdl'); $server->setClass('CTest'); $server->handle(); ?>
定义WebService 访问文件
<?php //error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE); header("Content-Type: text/html;charset=utf-8"); $client = new SoapClient("http://192.168.1.11:88/testServer.php?wsdl"); $str = '{"a":2,"b":28}'; //调用方法一 //$r = $client->Add(array('oParams'=>$str)); //数组 //调用方法二 $pParams->oParams = $str; $r = $client->__call('Add',array($pParams));//这个得是对象 $r = object_array($r); ?>
定义xml文件
<?xml version="1.0" encoding="UTF-8" ?> <wsdl:definitions targetNamespace="http://tempuri.org/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://localhost/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:types> <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/"> <s:element name="AddRequest"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="oParams" type="s:string" /> </s:sequence> </s:complexType> </s:element> <s:element name="AddResponse"> <s:complexType> <s:sequence> <s:element minOccurs="0" maxOccurs="1" name="AddResult" type="s:string" /> </s:sequence> </s:complexType> </s:element> </s:schema> </wsdl:types> <wsdl:message name="AddSoapIn"> <wsdl:part name="parameters" element="tns:AddRequest" /> </wsdl:message> <wsdl:message name="AddSoapOut"> <wsdl:part name="parameters" element="tns:AddResponse" /> </wsdl:message> <wsdl:portType name="CTestSoap"> <wsdl:operation name="Add"> <wsdl:input message="tns:AddSoapIn" /> <wsdl:output message="tns:AddSoapOut" /> </wsdl:operation> </wsdl:portType> <wsdl:binding name="CTestSoap" type="tns:CTestSoap"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="Add"> <soap:operation soapAction="http://localhost/Add" style="document" /> <wsdl:input> <soap:body use="literal" /> </wsdl:input> <wsdl:output> <soap:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:binding name="CTestSoap12" type="tns:CTestSoap"> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="Add"> <soap12:operation soapAction="http://localhost/Add" style="document" /> <wsdl:input> <soap12:body use="literal" /> </wsdl:input> <wsdl:output> <soap12:body use="literal" /> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="CTest"> <wsdl:port name="CTestSoap" binding="tns:CTestSoap"> <soap:address location="http://127.0.0.1/testServer.php?wsdl " /> </wsdl:port> <wsdl:port name="CTestSoap12" binding="tns:CTestSoap12"> <soap12:address location="http://127.0.0.1/testServer.php?wsdl " /> </wsdl:port> </wsdl:service> </wsdl:definitions>
直接访问即可