来段
企业级应用吧
,主要是讲
PHP5对webservice的一些实现(以下的程序可以被JAVA
,NET,C等正常调用)
国内用
PHP写WebService的真的很少,网上资料也没多少,公司的项目开发过程中,经历了不少这方面的东西,写出来以供大家参考(谢谢老农提供的WSDL和程序文件)
客户端
<?php
header("Content-Type:text/html;charset=utf-8");
/*
*指定WebService路径并初始化一个WebService客户端
*/
$ws="
http://soap/soapCspMessage.php?wsdl";
$client=newSoapClient($ws,array('trace'=>1,'uri'=>'http://www.zxsv.com/SoapDiscovery/'));
/*
*获取SoapClient对象引用的服务所提供的所有方法
*/
echo("SOAP
服务器提供的开放函数:");
echo('<pre>');
var_dump($client->__getFunctions());
echo('</pre>');
echo("SOAP
服务器提供的Type:");
echo('<pre>');
var_dump($client->__getTypes());
echo('</pre>');
echo("执行GetGUIDNode的结果:");
//$users=$client->GetUsers();
//var_dump($HelloWorld);
$parameters=array('uname'=>'zxsv',"upassword"=>'123');
$out=$client->HelloWorld($parameters);
$datadb=$out->HelloWorldResponse;
var_dump($out);
?>
服务端
<?php
classMember
{
public$UserId;
public$Name;
publicfunction__construct($parmas){
$this->UserId=$parmas[0];
$this->Name=$parmas[1];
}
}
$servidorSoap=newSoapServer('testphp.xml',array('uri'=>'http://www.TestPHP.com/','encoding'=>'utf-8','soap_version'=>SOAP_1_2));
$servidorSoap->setClass(Testphp);
$servidorSoap->handle();
classTestphp{
publicfunctionHelloWorld($uid){
returnarray('HelloWorldResult'=>"mystring".$uid->{'uname'}.'and'.$uid->{'upassword'});
}
publicfunctionGetMember($uid){
$s=array();
for($i=0;$i<$uid->{'uid'};$i++){
$s[]=&newMember(array($i,$uid->{'uname'}.'我测试'.$i));
}
returnarray('GetMemberResult'=>$s);
}
}
?>
到这里应该都看的懂吧
下面是WSDL文件
<?xmlversion="1.0"encoding="utf-8"?>
<wsdl:definitionsxmlns:soap="
http://schemas.xmlsoap.org/wsdl/soap/"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://www.TestPHP.com/"xmlns:s="http://www.w3.org/2001/XMLSchema"xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"targetNamespace="http://www.TestPHP.com/"xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schemaelementFormDefault="qualified"targetNamespace="
http://www.TestPHP.com/">
<s:elementname="HelloWorld">
<s:complexType>
<s:sequence>
<s:elementminOccurs="0"maxOccurs="1"name="uname"type="s:string"/>
<s:elementminOccurs="0"maxOccurs="1"name="upassword"type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:elementname="HelloWorldResponse">
<s:complexType>
<s:sequence>
<s:elementminOccurs="0"maxOccurs="1"name="HelloWorldResult"type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:elementname="GetMember">
<s:complexType>
<s:sequence>
<s:elementminOccurs="1"maxOccurs="1"name="uid"type="s:int"/>
<s:elementminOccurs="0"maxOccurs="1"name="uname"type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:elementname="GetMemberResponse">
<s:complexType>
<s:sequence>
<s:elementminOccurs="0"maxOccurs="1"name="GetMemberResult"type="tns:ArrayOfMember"/>
</s:sequence>
</s:complexType>
</s:element>
<s:complexTypename="ArrayOfMember">
<s:sequence>
<s:elementminOccurs="0"maxOccurs="unbounded"name="Member"nillable="true"type="tns:Member"/>
</s:sequence>
</s:complexType>
<s:complexTypename="Member">
<s:sequence>
<s:elementminOccurs="1"maxOccurs="1"name="UserId"type="s:int"/>
<s:elementminOccurs="0"maxOccurs="1"name="Name"type="s:string"/>
</s:sequence>
</s:complexType>
</s:schema>
</wsdl:types>
<wsdl:messagename="HelloWorldSoapIn">
<wsdl:partname="parameters"element="tns:HelloWorld"/>
</wsdl:message>
<wsdl:messagename="HelloWorldSoapOut">
<wsdl:partname="parameters"element="tns:HelloWorldResponse"/>
</wsdl:message>
<wsdl:messagename="GetMemberSoapIn">
<wsdl:partname="parameters"element="tns:GetMember"/>
</wsdl:message>
<wsdl:messagename="GetMemberSoapOut">
<wsdl:partname="parameters"element="tns:GetMemberResponse"/>
</wsdl:message>
<wsdl:portTypename="TestPHPSoap">
<wsdl:operationname="HelloWorld">
<wsdl:inputmessage="tns:HelloWorldSoapIn"/>
<wsdl:outputmessage="tns:HelloWorldSoapOut"/>
</wsdl:operation>
<wsdl:operationname="GetMember">
<wsdl:inputmessage="tns:GetMemberSoapIn"/>
<wsdl:outputmessage="tns:GetMemberSoapOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:bindingname="TestPHPSoap"type="tns:TestPHPSoap">
<soap:bindingtransport="
http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operationname="HelloWorld">
<soap:operationsoapAction="
http://www.TestPHP.com/HelloWorld"/>
<wsdl:input>
<soap:bodyuse="literal"/>
</wsdl:input>
<wsdl:output>
<soap:bodyuse="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operationname="GetMember">
<soap:operationsoapAction="
http://www.TestPHP.com/GetMember"/>
<wsdl:input>
<soap:bodyuse="literal"/>
</wsdl:input>
<wsdl:output>
<soap:bodyuse="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:bindingname="TestPHPSoap12"type="tns:TestPHPSoap">
<soap12:bindingtransport="
http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operationname="HelloWorld">
<soap12:operationsoapAction="
http://www.TestPHP.com/HelloWorld"/>
<wsdl:input>
<soap12:bodyuse="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:bodyuse="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operationname="GetMember">
<soap12:operationsoapAction="
http://www.TestPHP.com/GetMember"/>
<wsdl:input>
<soap12:bodyuse="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:bodyuse="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:servicename="TestPHP">
<wsdl:portname="TestPHPSoap"binding="tns:TestPHPSoap">
<soap:addresslocation="
http://soap/goodwsdl/testphp.php"/>
</wsdl:port>
<wsdl:portname="TestPHPSoap12"binding="tns:TestPHPSoap12">
<soap12:addresslocation="
http://soap/goodwsdl/testphp.php"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
这里有返回的两个字段,一个是返回字符串,这个很好理解
<s:elementname="HelloWorld">
<s:complexType>
<s:sequence>
<s:elementminOccurs="0"maxOccurs="1"name="uname"type="s:string"/>
<s:elementminOccurs="0"maxOccurs="1"name="upassword"type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:elementname="HelloWorldResponse">
<s:complexType>
<s:sequence>
<s:elementminOccurs="0"maxOccurs="1"name="HelloWorldResult"type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
这一段就字符串的
那返回数组的就比较麻烦了,我和老农搞了一两周才发现是WSDL文件写错了,看下面的一段
<s:elementname="GetMember">
<s:complexType>
<s:sequence>
<s:elementminOccurs="1"maxOccurs="1"name="uid"type="s:int"/>
<s:elementminOccurs="0"maxOccurs="1"name="uname"type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:elementname="GetMemberResponse">
<s:complexType>
<s:sequence>
<s:elementminOccurs="0"maxOccurs="1"name="GetMemberResult"type="tns:ArrayOfMember"/>
</s:sequence>
</s:complexType>
</s:element>
<s:complexTypename="ArrayOfMember">
<s:sequence>
<s:elementminOccurs="0"maxOccurs="unbounded"name="Member"nillable="true"type="tns:Member"/>
</s:sequence>
</s:complexType>
<s:complexTypename="Member">
<s:sequence>
<s:elementminOccurs="1"maxOccurs="1"name="UserId"type="s:int"/>
<s:elementminOccurs="0"maxOccurs="1"name="Name"type="s:string"/>
</s:sequence>
</s:complexType>
第一段GetMember是输入,最重要的是GetMemberResponse这段,看type="tns:ArrayOfMember"这里,返回一个数组,WSDL中定义了ArrayOf这个,后面的就简单了,ArrayOfMember的类型是type="tns:Member",从name="Member"得到要返回的数组,完工
。