PHP中soap的使用例子

來源:互聯網
上載者:User

標籤:style   blog   http   ar   io   color   os   使用   sp   

原文:PHP中soap的使用例子

PHP 使用soap有兩種方式。

一、用wsdl檔案

伺服器端。

<?phpclass service{  public function HelloWorld()   {      return  "Hello";   }  public  function Add($a,$b)   {      return $a+$b;   }}$server=new SoapServer(‘soap.wsdl‘,array(‘soap_version‘ => SOAP_1_2));$server->setClass("service");$server->handle();?>
資源描述檔案,可以用工具(zend studio)產生。其實就是一個xml檔案。

<?xml version="1.0" encoding="UTF-8"?><wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost/interface/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="soap" targetNamespace="http://localhost/interface/">  <wsdl:types>    <xsd:schema targetNamespace="http://localhost/interface/">      <xsd:element name="HelloWorld">        <xsd:complexType>          <xsd:sequence>            <xsd:element name="in" type="xsd:string"/>          </xsd:sequence>        </xsd:complexType>      </xsd:element>      <xsd:element name="HelloWorldResponse">        <xsd:complexType>          <xsd:sequence>            <xsd:element name="out" type="xsd:string"/>          </xsd:sequence>        </xsd:complexType>      </xsd:element>      <xsd:element name="Add">      <xsd:complexType>      <xsd:sequence>      <xsd:element name="in" type="xsd:int"></xsd:element>      </xsd:sequence>      </xsd:complexType>      </xsd:element>      <xsd:element name="AddResponse">      <xsd:complexType>      <xsd:sequence>      <xsd:element name="out" type="xsd:int"></xsd:element>      </xsd:sequence>      </xsd:complexType>      </xsd:element>    </xsd:schema>  </wsdl:types>   <wsdl:message name="AddRequest">   <wsdl:part name="a" type="xsd:int"></wsdl:part>  <wsdl:part name="b" type="xsd:int"></wsdl:part>  </wsdl:message>  <wsdl:message name="AddResponse">  <wsdl:part name="c" type="xsd:int"></wsdl:part>  </wsdl:message>  <wsdl:portType name="TestSoap">     <wsdl:operation name="Add">    <wsdl:input message="tns:AddRequest"></wsdl:input>    <wsdl:output message="tns:AddResponse"></wsdl:output>    </wsdl:operation>  </wsdl:portType>  <wsdl:binding name="soapSOAP" type="tns:TestSoap">  <soap:binding style="document"  transport="http://schemas.xmlsoap.org/soap/http" />  <wsdl:operation name="Add">  <soap:operation soapAction="http://localhost/interface/Add" />  <wsdl:input>  <soap:body use="literal"  namespace="http://localhost/interface/" />  </wsdl:input>  <wsdl:output>  <soap:body use="literal"  namespace="http://localhost/interface/" />  </wsdl:output>  </wsdl:operation>  </wsdl:binding>  <wsdl:service name="TestSoap">    <wsdl:port binding="tns:soapSOAP" name="soapSOAP">      <soap:address location="http://localhost/interface/myservice.php"/>    </wsdl:port>  </wsdl:service></wsdl:definitions>
用戶端調用
<?php$soap = new SoapClient(‘http://localhost/interface/soap.wsdl‘);echo $soap->Add(1,2);?>

二、不用wsdl檔案

伺服器端

<?phpclass service{  public function HelloWorld()   {      return  "Hello";   }  public  function Add($a,$b)   {      return $a+$b;   }}$server=new SoapServer(null,array(‘uri‘ => "abcd"));$server->setClass("service");$server->handle();?>
用戶端
<?phptry{$soap = new SoapClient(null,array("location" => "http://localhost/interface/soap.php","uri"      => "abcd",  //資源描述符伺服器和用戶端必須對應"style"    => SOAP_RPC,"use"      => SOAP_ENCODED   ));echo $soap->Add(1,2);}catch(Exction $e){echo print_r($e->getMessage(),true);}?>

PHP中soap的使用例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.