首先新建一个项目
在项目中新建下面这些文件
php类文件 test.php
<?php
class test {
public function __construct()
{
}
public function add($name,$age)
{
$result = array('REV'=>false);
$result['REV'] = true;
$result['DATA'] = 1;
$result = json_encode($result);
return $result;
}
public function del($id)
{
$result = false;
return $result;
}
public function getlist($type)
{
$result = array(
array('name'=>'张三','age'=>18),
array('name'=>'李四','age'=>20),
array('name'=>'jms','age'=>10),
array('name'=>'jk陈','age'=>8),
);
$result = json_encode($result);
return $result;
}
} ?>
使用zendstudio生成wsdl文件
生成的文件格式如下
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost/t/ws" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="test_server" targetNamespace="http://localhost/t/ws">
<wsdl:types>
<xsd:schema targetNamespace="http://localhost/t/ws">
<xsd:element name="getlist">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="in" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="getlistResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="out" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="getlistRequest">
<wsdl:part name="type" type="xsd:int"/>
</wsdl:message>
<wsdl:message name="getlistResponse">
<wsdl:part name="resultRespose" type="xsd:string"/>
</wsdl:message>
<wsdl:portType name="test_server">
<wsdl:operation name="getlist">
<wsdl:input message="tns:getlistRequest"/>
<wsdl:output message="tns:getlistResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="test_serverSOAP" type="tns:test_server">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="getlist">
<soap:operation
soapAction="http://localhost/t/ws/NewOperation" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="test_server">
<wsdl:port binding="tns:test_serverSOAP" name="test_serverSOAP">
<soap:address location="http://localhost/t/ws/server.php"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
调用server的文件 server.php
<?php
ini_set("soap.wsdl_cache_enabled", "0"); //测试时打开防止soap缓存
include("test.php");
$Server=new SoapServer('test_server.wsdl'); //SoapServer
$Server->setClass("test");
$Server->handle();
?>
测试调用webserver wsdl的文件
<?php
ini_set("soap.wsdl_cache_enabled", "0");
//$url = 'http://localhost/t/ws/test_server.wsdl';
$url = 'http://localhost/t/ws/server.php?wsdl'; //两种url都可以
$client = new SoapClient($url);
$params = array('type'=>1);
$res = $client->__soapCall('getlist',array('parameters'=>$params));
var_dump($res);
?>
返回数据:
string(119) "[{"name":"\u5f20\u4e09","age":18},{"name":"\u674e\u56db","age":20},{"name":"jms","age":10},{"name":"jk\u9648","age":8}]"
调用成功