设为首页 收藏本站
查看: 673|回复: 0

[经验分享] PHP企业级应用之WebService篇

[复制链接]

尚未签到

发表于 2017-4-3 14:12:25 | 显示全部楼层 |阅读模式
  国内用PHP写WebService的真的很少,网上资料也没多少,公司的项目开发过程中,经历了不少这方面的东西,写出来以供大家参考(谢谢老农提供的WSDL和程序文件)
客户端
  <?phpheader ( "Content-Type: text/html; charset=utf-8" );/** 指定WebService路径并初始化一个WebService客户端*/$ws = "http://soap/soapCspMessage.php?wsdl";$client = new SoapClient ( $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);?>
  服务端
  <?phpclass 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://www.TestPHP.com/','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://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:schema elementFormDefault="qualified" targetNamespace="http://www.TestPHP.com/"><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://www.TestPHP.com/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://www.TestPHP.com/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://www.TestPHP.com/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://www.TestPHP.com/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"得到要返回的数组,完工。
  这是我这边的一份代码,你可以学习下,function getSSOLoginID($sessionID) {if ($sessionID == "") {return "";}// SOAP接続$soapclient =& new soapclient('http'//localhost/AuthCheckService?wsdl',true);$soapclient->debug_flag=true;$soapclient->timeout = 500;$soapclient->response_timeout = 500;if($err = $soapclient->getError()){execSSOLogWrite($err);return "";}$proxy = $soapclient->getProxy();if($err = $soapclient->getError()){execSSOLogWrite($err);return "";}$params = array('MISUMISESSION' => $sessionID);$result = $proxy->doAuthCheck($params);$status = $result['STATUS'];$userid = $result['USERID'];if ($status == "1") {return $userid;} else {return "";}}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-359683-1-1.html 上篇帖子: PHP实现翻页处理的类 下篇帖子: PHP常用字符串总结(待续)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表