PHP教程:Weervice最常用的两种方法_PHP技巧_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

PHP教程:Weervice最常用的两种方法

作者:黑客防线网安PHP教程基地 来源:黑客防线网安PHP教程基地 浏览次数:0

本篇关键词:方法常用教程/>
黑客防线网安网讯:  企业级应用,主要是讲PHP5对webservice的一些实现(以下的程序可以被JAVA,NET,C等正常调用)    国内用PHP写WebService的真的很少,网上资料也没多少,公司的项目开发过程中,经历了...
  企业级应用主要是讲PHP5对webservice的一些实现(以下的程序可以被JAVANET,C等正常调用)
  
  国内用PHP写WebService的真的很少,网上资料也没多少,公司的项目开发过程中,经历了不少这方面的东西,写出来以供大家参考(谢谢老农提供的WSDL和程序文件)
  
  客户端
  
  代码:
  
  01.<?php
  
  02.header("Content-Type:text/html;charset=utf-8");
  
  03./*
  
  04.*指定WebService路径并初始化一个WebService客户端
  
  05.*/
  
  06.$ws="http://soap/soapCspMessage.php?wsdl";
  
  07.$client=newSoapClient($ws,array('trace'=>1,'uri'=>'http://www.zxsv.com/SoapDiscovery/'));
  
  08./*
  
  09.*获取SoapClient对象引用的服务所提供的所有方法
  
  10.*/
  
  11.echo("SOAP服务器提供的开放函数:");
  
  12.echo('<pre>');
  
  13.var_dump($client->__getFunctions());
  
  14.echo('</pre>');
  
  15.echo("SOAP服务器提供的Type:");
  
  16.echo('<pre>');
  
  17.var_dump($client->__getTypes());
  
  18.echo('</pre>');
  
  19.echo("执行GetGUIDNode的结果:");
  
  20.//$users=$client->GetUsers();
  
  21.//var_dump($HelloWorld);
  
  22.$parameters=array('uname'=>'zxsv',"upassword"=>'123');
  
  23.$out=$client->HelloWorld($parameters);
  
  24.$datadb=$out->HelloWorldResponse;
  
  25.var_dump($out);
  
  26.?>
  
  服务端
  
  代码:
  
  01.<?php
  
  02.classMember
  
  03.{
  
  04.public$UserId;
  
  05.public$Name;
  
  06.publicfunction__construct($parmas){
  
  07.$this->UserId=$parmas[0];
  
  08.$this->Name=$parmas[1];
  
  09.}
  
  10.}
  
  11.$servidorSoap=newSoapServer('testphp.xml',array('uri'=>'http://www.TestPHP.com/','encoding'=>'utf-8','soap_version'=>SOAP_1_2));
  
  12.$servidorSoap->setClass(Testphp);
  
  13.$servidorSoap->handle();
  
  14.classTestphp{
  
  15.publicfunctionHelloWorld($uid){
  
  16.returnarray('HelloWorldResult'=>"mystring".$uid->{'uname'}.'and'.$uid->{'upassword'});
  
  17.}
  
  18.publicfunctionGetMember($uid){
  
  19.$s=array();
  
  20.for($i=0;$i<$uid->{'uid'};$i++){
  
  21.$s[]=&amp;newMember(array($i,$uid->{'uname'}.'我测试'.$i));
  
  22.}
  
  23.returnarray('GetMemberResult'=>$s);
  
  24.}
  
  25.}
  
  26.?>
  
  到这里应该都看的懂吧
  
  下面是WSDL文件
  
  代码:
  
  001.<?xmlversion="1.0"encoding="utf-8"?>
  
  002.<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/">
  
  003.<wsdl:types>
  
  004.<s:schemaelementFormDefault="qualified"targetNamespace="http://www.TestPHP.com/">
  
  005.<s:elementname="HelloWorld">
  
  006.<s:complexType>
  
  007.<s:sequence>
  
  008.<s:elementminOccurs="0"maxOccurs="1"name="uname"type="s:string"/>
  
  009.<s:elementminOccurs="0"maxOccurs="1"name="upassword"type="s:string"/>
  
  010.</s:sequence>
  
  011.</s:complexType>
  
  012.</s:element>
  
  013.<s:elementname="HelloWorldResponse">
  
  014.<s:complexType>
  
  015.<s:sequence>
  
  016.<s:elementminOccurs="0"maxOccurs="1"name="HelloWorldResult"type="s:string"/>
  
  017.</s:sequence>
  
  018.</s:complexType>
  
  019.</s:element>
  
  020.<s:elementname="GetMember">
  
  021.<s:complexType>
  
  022.<s:sequence>
  
  023.<s:elementminOccurs="1"maxOccurs="1"name="uid"type="s:int"/>
  
  024.<s:elementminOccurs="0"maxOccurs="1"name="uname"type="s:string"/>
  
  025.</s:sequence>
  
  026.</s:complexType>
  
  027.</s:element>
  
  028.<s:elementname="GetMemberResponse">
  
  029.<s:complexType>
  
  030.<s:sequence>
  
  031.<s:elementminOccurs="0"maxOccurs="1"name="GetMemberResult"type="tns:ArrayOfMember"/>
  
  032.</s:sequence>
  
  033.</s:complexType>
  
  034.</s:element>
  
  035.<s:complexTypename="ArrayOfMember">
  
  036.<s:sequence>
  
  037.<s:elementminOccurs="0"maxOccurs="unbounded"name="Member"nillable="true"type="tns:Member"/>
  
  038.</s:sequence>
  
  039.</s:complexType>
  
  040.<s:complexTypename="Member">
  
  041.<s:sequence>
  
  042.<s:elementminOccurs="1"maxOccurs="1"name="UserId"type="s:int"/>
  
  043.<s:elementminOccurs="0"maxOccurs="1"name="Name"type="s:string"/>
  
  044.</s:sequence>
  
  045.</s:complexType>
  
  046.</s:schema>
  
  047.</wsdl:types>
  
  048.<wsdl:messagename="HelloWorldSoapIn">
  
  049.<wsdl:partname="parameters"element="tns:HelloWorld"/>
  
  050.</wsdl:message>
  
  051.<wsdl:messagename="HelloWorldSoapOut">
  
  052.<wsdl:partname="parameters"element="tns:HelloWorldResponse"/>
  
  053.</wsdl:message>
  
  054.<wsdl:messagename="GetMemberSoapIn">
  
  055.<wsdl:partname="parameters"element="tns:GetMember"/>
  
  056.</wsdl:message>
  
  057.<wsdl:messagename="GetMemberSoapOut">
  
  058.<wsdl:partname="parameters"element="tns:GetMemberResponse"/>
  
  059.</wsdl:message>
  
  060.<wsdl:portTypename="TestPHPSoap">
  
  061.<wsdl:operationname="HelloWorld">
  
  062.<wsdl:inputmessage="tns:HelloWorldSoapIn"/>
  
  063.<wsdl:outputmessage="tns:HelloWorldSoapOut"/>
  
  064.</wsdl:operation>
  
  065.<wsdl:operationname="GetMember">
  
  066.<wsdl:inputmessage="tns:GetMemberSoapIn"/>
  
  067.<wsdl:outputmessage="tns:GetMemberSoapOut"/>
  
  068.</wsdl:operation>
  
  069.</wsdl:portType>
  
  070.<wsdl:bindingname="TestPHPSoap"type="tns:TestPHPSoap">
  
  071.<soap:bindingtransport="http://schemas.xmlsoap.org/soap/http"/>
  
  072.<wsdl:operationname="HelloWorld">
  
  073.<soap:operationsoapAction="http://www.TestPHP.com/HelloWorld"/>
  
  074.<wsdl:input>
  
  075.<soap:bodyuse="literal"/>
  
  076.</wsdl:input>
  
  077.<wsdl:output>
  
  078.<soap:bodyuse="literal"/>
  
  079.</wsdl:output>
  
  080.</wsdl:operation>
  
  081.<wsdl:operationname="GetMember">
  
  082.<soap:operationsoapAction="http://www.TestPHP.com/GetMember"/>
  
  083.<wsdl:input>
  
  084.<soap:bodyuse="literal"/>
  
  085.</wsdl:input>
  
  086.<wsdl:output>
  
  087.<soap:bodyuse="literal"/>
  
  088.</wsdl:output>
  
  089.</wsdl:operation>
  
  090.</wsdl:binding>
  
  091.<wsdl:bindingname="TestPHPSoap12"type="tns:TestPHPSoap">
  
  092.<soap12:bindingtransport="http://schemas.xmlsoap.org/soap/http"/>
  
  093.<wsdl:operationname="HelloWorld">
  
  094.<soap12:operationsoapAction="http://www.TestPHP.com/HelloWorld"/>
  
  095.<wsdl:input>
  
  096.<soap12:bodyuse="literal"/>
  
  097.</wsdl:input>
  
  098.<wsdl:output>
  
  099.<soap12:bodyuse="literal"/>
  
  100.</wsdl:output>
  
  101.</wsdl:operation>
  
  102.<wsdl:operationname="GetMember">
  
  103.<soap12:operationsoapAction="http://www.TestPHP.com/GetMember"/>
  
  104.<wsdl:input>
  
  105.<soap12:bodyuse="literal"/>
  
  106.</wsdl:input>
  
  107.<wsdl:output>
  
  108.<soap12:bodyuse="literal"/>
  
  109.</wsdl:output>
  
  110.</wsdl:operation>
  
  111.</wsdl:binding>
  
  112.<wsdl:servicename="TestPHP">
  
  113.<wsdl:portname="TestPHPSoap"binding="tns:TestPHPSoap">
  
  114.<soap:addresslocation="http://soap/goodwsdl/testphp.php"/>
  
  115.</wsdl:port>
  
  116.<wsdl:portname="TestPHPSoap12"binding="tns:TestPHPSoap12">
  
  117.<soap12:addresslocation="http://soap/goodwsdl/testphp.php"/>
  
  118.</wsdl:port>
  
  119.</wsdl:service>
  
  120.</wsdl:definitions>
  
  这里有返回的两个字段,一个是返回字符串,这个很好理解
  
  01.<s:elementname="HelloWorld">
  
  02.<s:complexType>
  
  03.<s:sequence>
  
  04.<s:elementminOccurs="0"maxOccurs="1"name="uname"type="s:string"/>
  
  05.<s:elementminOccurs="0"maxOccurs="1"name="upassword"type="s:string"/>
  
  06.</s:sequence>
  
  07.</s:complexType>
  
  08.</s:element>
  
  09.<s:elementname="HelloWorldResponse">
  
  10.<s:complexType>
  
  11.<s:sequence>
  
  12.<s:elementminOccurs="0"maxOccurs="1"name="HelloWorldResult"type="s:string"/>
  
  13.</s:sequence>
  
  14.</s:complexType>
  
  15.</s:element>
  
  这一段就字符串的
  
  那返回数组的就比较麻烦了,我和老农搞了一两周才发现是WSDL文件写错了,看下面的一段
  
  01.<s:elementname="GetMember">
  
  02.<s:complexType>
  
  03.<s:sequence>
  
  04.<s:elementminOccurs="1"maxOccurs="1"name="uid"type="s:int"/>
  
  05.<s:elementminOccurs="0"maxOccurs="1"name="uname"type="s:string"/>
  
  06.</s:sequence>
  
  07.</s:complexType>
  
  08.</s:element>
  
  09.<s:elementname="GetMemberResponse">
  
  10.<s:complexType>
  
  11.<s:sequence>
  
  12.<s:elementminOccurs="0"maxOccurs="1"name="GetMemberResult"type="tns:ArrayOfMember"/>
  
  13.</s:sequence>
  
  14.</s:complexType>
  
  15.</s:element>
  
  16.<s:complexTypename="ArrayOfMember">
  
  17.<s:sequence>
  
  18.<s:elementminOccurs="0"maxOccurs="unbounded"name="Member"nillable="true"type="tns:Member"/>
  
  19.</s:sequence>
  
  20.</s:complexType>
  
  21.<s:complexTypename="Member">
  
  22.<s:sequence>
  
  23.<s:elementminOccurs="1"maxOccurs="1"name="UserId"type="s:int"/>
  
  24.<s:elementminOccurs="0"maxOccurs="1"name="Name"type="s:string"/>
  
  25.</s:sequence>
  
  26.</s:complexType>
  
  第一段GetMember是输入,最重要的是GetMemberResponse这段,看type=”tns:ArrayOfMember”这里,返回一个数组,WSDL中定义了ArrayOf这个,后面的就简单了,ArrayOfMember的类型是type=”tns:Member”,从name=”Member”得到要返回的数组,完工
  
  
    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-17552-1.html
网站维护教程更新时间:2012-09-21 05:21:02  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer