SOAP简单例子

soap_client.php

<?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

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 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>
 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值