公司调试需要用到WSDL,所以测试一下
一:写一个测试类,简单的封装几个方法<?php class CTest{ public function __construct() { } public function add($a, $b) { return $a+$b; } public function cut($a, $b) { return $a-$b; } }二:生成WSDL文件(用zend studio)
<?xml version='1.0' encoding='UTF-8'?> <definitions name="Config1" targetNamespace="urn:Config1" xmlns:typens="urn:Config1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"> <message name="__construct"/> <message name="__constructResponse"/> <message name="add"> <part name="a"/> <part name="b"/> </message> <message name="addResponse"> <part name="addReturn"/> </message> <message name="cut"> <part name="a"/> <part name="b"/> </message> <message name="cutResponse"> <part name="cutReturn"/> </message> <portType name="CTestPortType"> <operation name="__construct"> <input message="typens:__construct"/> <output message="typens:__constructResponse"/> </operation> <operation name="add"> <input message="typens:add"/> <output message="typens:addResponse"/> </operation> <operation name="cut"> <input message="typens:cut"/> <output message="typens:cutResponse"/> </operation> </portType> <binding name="CTestBinding" type="typens:CTestPortType"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="__construct"> <soap:operation soapAction="urn:CTestAction"/> <input> <soap:body namespace="urn:Config1" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <soap:body namespace="urn:Config1" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> <operation name="add"> <soap:operation soapAction="urn:CTestAction"/> <input> <soap:body namespace="urn:Config1" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <soap:body namespace="urn:Config1" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> <operation name="cut"> <soap:operation soapAction="urn:CTestAction"/> <input> <soap:body namespace="urn:Config1" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <soap:body namespace="urn:Config1" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> </binding> <service name="Config1Service"> <port name="CTestPort" binding="typens:CTestBinding"> <soap:address location=""/> </port> </service> </definitions>以上所有文件是放在服务器上
<?php ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache $wsdl = 'http://www.work.com/wsdl/ceshi.wsdl'; //换成你本地的地址,这是我的虚拟域名 //$soap = new SoapClient($wsdl, array( 'trace'=>true,'cache_wsdl'=>'WSDL_CACHE_NONE', 'soap_version' => 'SOAP_1_1')); $soap = new SoapClient($wsdl); //这样也可以实例化soap //$soap = new SoapClient('http://www.work.com/wsdl/server.php?wsdl'); try{ //调用方法一: $result=$soap->cut(7,2); //调用方法二: //$result = $soap->__call('add',array(1,2)); }catch(Exception $e) { echo "Exception: " . $e->getMessage(); } echo $result;访问client.php
<?php include_once('test.class.php'); $server = new SoapServer('ceshi.wsdl'); $server->setClass('CTest'); $server->handle();并且修改ceshi.wsdl:
<soap:address location="http://www.work.com/wsdl/server.php"/>之前生成的location为空,改成你服务器的server.php的访问路径
$server = new SoapServer(null,array('uri'=>'abcd','encoding'=>'UTF-8'));------------client.php---------------
$soap = new SoapClient(null,array( 'location'=>'http://www.work.com/wsdl/server.php', 'uri'=>'abcd', 'encoding' => 'UTF-8', ));将两个类实例化的参数改下即可
ps:以上全部测试通过,特分享出来,如有问题请留言。
已有0条评论