公司调试需要用到WSDL,所以测试一下
一:写一个测试类,简单的封装几个方法
————test.class.php—————
<?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)
————ceshi.wsdl—————
<?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>
以上所有文件是放在服务器上
三:本地新建一个client.php文件
————client.php—————
<?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
报错:Exception: Unable to parse URL
四:服务器上新建一个server.php文件
————server.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的访问路径
再次访问client.php
显示:5
成功了,网上大把的人都不可以访问wsdl,都是因为wsdl的location为空的缘故
当然php要开启soap支持
五:如果没有生成wsdl,也是可以的,不过要修改两个地方
————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',
));
将两个类实例化的参数改下即可
uri为必要参数,值随便给。
至于uri,网上的说法:
命名空间 URI 为给定的 SOAP 操作指定用于组成 SOAP 请求封装的 XML 命名空间。如果命名空间 URI 未被定义,将使用 URL 子句中的域组件。
服务器端的 SOAP 处理器使用此 URI 来了解请求的消息主体中各种实体的名称。
ps:以上全部测试通过,特分享出来,如有问题请留言。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/98459.html