<?php
$client = new soapclient('http://www.phptest.com/soap/soap_server.php?wsdl');
//$client = new soapclient('http://localhost/php/soap/math.wsdl');
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('math.wsdl', array('soap_version' => soap_1_2));
$server->setclass("math");
$server->handle();
?>
math.wsdl (可以通过zend studio生成)
xml代码
<?xml version='1.0' encoding='utf-8'?>
<!-- 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>