PHP企業級應用之WebService篇

來源:互聯網
上載者:User

簡介:這是PHP企業級應用之WebService篇的詳細頁面,介紹了和php,有關的知識、技巧、經驗,和一些php源碼等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=323149' scrolling='no'>

來段企業級應用吧,主要是講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 = 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="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>

 

“PHP企業級應用之WebService篇”的更多相關文章 》

愛J2EE關注Java邁克爾傑克遜視頻站JSON線上工具

http://biancheng.dnbcw.info/php/323149.html pageNo:16

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.