Einfaches SOAP-Beispiel

巴扎黑
Freigeben: 2016-11-29 10:45:16
Original
962 Leute haben es durchsucht

  
<?php  
  $client = new SoapClient(&#39;http://www.phptest.com/soap/soap_server.php?WSDL&#39;);  
  //$client = new SoapClient(&#39;http://localhost/php/soap/math.wsdl&#39;);  
  try {  
    $result = $client->div(10, 2); // will cause a Soap Fault if divide by zero  
    print "The answer is: $result";  
  } catch(SoapFault $e) {  
    print "Sorry an error was caught executing your request: {$e->getMessage()}";  
  }  
  
?>  
 
soap_server.php
Php代码  
<?php  
  
class math {  
  
  public function add($a, $b) {  
    return $a + $b;  
  }  
    
  public function div($a, $b) {  
    if($b == 0) {  
      throw new SoapFault(-1, "Cannot divide by zero!");  
    }  
    return $a / $b;  
  }    
}  
$server = new SoapServer(&#39;math.wsdl&#39;, array(&#39;soap_version&#39; => SOAP_1_2));  
$server->setClass("math");  
$server->handle();  
    
?>  
 
math.wsdl (可以通过zend studio生成)
Xml代码  
<?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?>  
  
<!-- WSDL file generated by Zend Studio. -->  
  
<definitions name="math" targetNamespace="urn:math" xmlns:typens="urn:math" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">  
    <message name="add">  
        <part name="a" type="xsd:integer"/>  
        <part name="b" type="xsd:integer"/>  
    </message>  
    <message name="addResponse">  
        <part name="addReturn" type="xsd:integer"/>  
    </message>  
    <message name="div">  
        <part name="a" type="xsd:integer"/>  
        <part name="b" type="xsd:integer"/>  
    </message>  
    <message name="divResponse">  
        <part name="divReturn" type="xsd:double"/>  
    </message>  
    <portType name="mathPortType">  
        <documentation>  
            A simple math utility class  
        </documentation>  
        <operation name="add">  
            <documentation>  
                Add two integers together  
            </documentation>  
            <input message="typens:add"/>  
            <output message="typens:addResponse"/>  
        </operation>  
        <operation name="div">  
            <documentation>  
                Div two integers from each other  
            </documentation>  
            <input message="typens:div"/>  
            <output message="typens:divResponse"/>  
        </operation>  
    </portType>  
    <binding name="mathBinding" type="typens:mathPortType">  
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>  
        <operation name="add">  
            <soap:operation soapAction="urn:mathAction"/>  
            <input>  
                <soap:body namespace="urn:math" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>  
            </input>  
            <output>  
                <soap:body namespace="urn:math" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>  
            </output>  
        </operation>  
        <operation name="div">  
            <soap:operation soapAction="urn:mathAction"/>  
            <input>  
                <soap:body namespace="urn:math" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>  
            </input>  
            <output>  
                <soap:body namespace="urn:math" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>  
            </output>  
        </operation>  
    </binding>  
    <service name="mathService">  
        <port name="mathPort" binding="typens:mathBinding">  
            <soap:address location="http://www.phptest.com/soap/soap_server.php"/>  
        </port>  
    </service>  
</definitions>
Nach dem Login kopieren

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!