PHP企业级应用之WebService篇

网络整理 - 08-07
来段企业级应用吧,主要是讲PHP5对webservice的一些实现(以下的程序可以被JAVA,NET,C等正常调用)

国内用PHP写WebService的真的很少,网上资料也没多少,公司的项目开发过程中,经历了不少这方面的东西,写出来以供大家参考(谢谢老农提供的WSDL和程序文件)

客户端

<?php
header ( "Content-Type: text/html; charset=utf-8" );
/*
* 指定WebService路径并初始化一个WebService客户端
*/
$ws = "?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);
?>

服务端

<?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://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="" xmlns:tm="" xmlns:soapenc="" xmlns:mime="" xmlns:tns="" xmlns:s="" xmlns:soap12="" xmlns:http="" targetNamespace="" xmlns:wsdl="">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="">
      <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=&quo

[1] [2] 下一页