PHP企业级应用之WebService篇
PHP #webservice2012-05-17 13:34
国内用PHP写WebService的真的很少,网上资料也没多少,公司的项目开发过程中,经历了不少这方面的东西,写出来以供大家参考。
客户端
<?php
header ( "Content-Type: text/html; charset=utf-8" );
/*
* 指定WebService路径并初始化一个WebService客户端
*/
$ws = "http://soap/soapCspMessage.php?wsdl";
$client = new SoapClient ( $ws, array ('trace' => 1, 'uri' => 'http://yige.org/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
class Member
{
public $UserId;
public $Name;
public function __construct($parmas){
$this- >UserId = $parmas[0];
$this- >Name = $parmas[1];
}
}
$servidorSoap = new SoapServer('testphp.xml',array('uri' => 'http://yige.org/','encoding'=>'utf-8','soap_version' => SOAP_1_2 ));
$servidorSoap->setClass(Testphp);
$servidorSoap->handle();
class Testphp {
public function HelloWorld($uid){
return array('HelloWorldResult'= >"mystring".$uid->{'uname'}.' and '.$uid->{'upassword'});
}
public function GetMember($uid){
$s=array();
for($i=0;$i <$uid->{'uid'};$i++){
$s[] = &new Member(array($i, $uid->{'uname'}.'我测试'.$i));
}
return array('GetMemberResult'= >$s);
}
}
?>
到这里应该都看的懂吧
下面是WSDL文件
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns: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://yige.org/" 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://yige.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://yige.org/">
<s:element name="HelloWorld">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="uname" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="upassword" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="HelloWorldResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="GetMember">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="uid" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="uname" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="GetMemberResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetMemberResult" type="tns:ArrayOfMember" />
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="ArrayOfMember">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="Member" nillable="true" type="tns:Member" />
</s:sequence>
</s:complexType>
<s:complexType name="Member">
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="UserId" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="Name" type="s:string" />
</s:sequence>
</s:complexType>
</s:schema>
</wsdl:types>
<wsdl:message name="HelloWorldSoapIn">
<wsdl:part name="parameters" element="tns:HelloWorld" />
</wsdl:message>
<wsdl:message name="HelloWorldSoapOut">
<wsdl:part name="parameters" element="tns:HelloWorldResponse" />
</wsdl:message>
<wsdl:message name="GetMemberSoapIn">
<wsdl:part name="parameters" element="tns:GetMember" />
</wsdl:message>
<wsdl:message name="GetMemberSoapOut">
<wsdl:part name="parameters" element="tns:GetMemberResponse" />
</wsdl:message>
<wsdl:portType name="TestPHPSoap">
<wsdl:operation name="HelloWorld">
<wsdl:input message="tns:HelloWorldSoapIn" />
<wsdl:output message="tns:HelloWorldSoapOut" />
</wsdl:operation>
<wsdl:operation name="GetMember">
<wsdl:input message="tns:GetMemberSoapIn" />
<wsdl:output message="tns:GetMemberSoapOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="TestPHPSoap" type="tns:TestPHPSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="HelloWorld">
<soap:operation soapAction="http://yige.org/HelloWorld" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetMember">
<soap:operation soapAction="http://yige.org/GetMember" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="TestPHPSoap12" type="tns:TestPHPSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="HelloWorld">
<soap12:operation soapAction="http://yige.org/HelloWorld" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetMember">
<soap12:operation soapAction="http://yige.org/GetMember" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="TestPHP">
<wsdl:port name="TestPHPSoap" binding="tns:TestPHPSoap">
<soap:address location="http://soap/goodwsdl/testphp.php" />
</wsdl:port>
<wsdl:port name="TestPHPSoap12" binding="tns:TestPHPSoap12">
<soap12:address location="http://soap/goodwsdl/testphp.php" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
这里有返回的两个字段,一个是返回字符串,这个很好理解
<s:element name="HelloWorld">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="uname" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="upassword" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="HelloWorldResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
这一段就字符串的
那返回数组的就比较麻烦了,我和老农搞了一两周才发现是WSDL文件写错了,看下面的一段
<s:element name="GetMember">
<s:complexType>
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="uid" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="uname" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="GetMemberResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetMemberResult" type="tns:ArrayOfMember" />
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="ArrayOfMember">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="Member" nillable="true" type="tns:Member" />
</s:sequence>
</s:complexType>
<s:complexType name="Member">
<s:sequence>
<s:element minOccurs="1" maxOccurs="1" name="UserId" type="s:int" />
<s:element minOccurs="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"得到要返回的数组,完工。
相关文章
- PHP实现汉字转区位码的代码 2012/05/16
- PHP网站开发方案流程 2012/05/16
- PHP自定义urldecode函数避免二次解码导致加号丢失 2012/05/16
- 说下PHP的urlencode/urldecode/addslashes重复操作 2012/05/15
- PHP用curl实现get和post 2012/05/15
- PHP判断访问用户是不是搜索引擎蜘蛛的函数 2012/05/13
- 默默教新手如何学习PHP 2012/05/13
- PHP加亮关键字的函数 2012/05/11
- PHP操作MySQL数据库的类 2012/05/11
- PHP判断一段字符串是不是utf8编码的函数 2012/05/10