PHP 使用soap有两种方式。

一、用wsdl文件

服务器端。

<?php
class 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文件

服务器端

<?php
class 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();
?>
客户端
<?php
try{
$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);
}
?>

稿源:微信开发www.qixoo.co m

最新文章

  1. bzoj4398:福慧双修
  2. Util应用程序框架公共操作类(十二):Lambda表达式公共操作类(三)
  3. React Native分析(index.ios.js)
  4. vs克隆新建团队项目
  5. 【POJ】【1067】取石子游戏
  6. 百练_4120 硬币(DP)
  7. Android imageview显示圆形图片
  8. DevExpress GridControl 列中显示图片
  9. 利用PCA来简化数据
  10. Android线程之异步消息处理机制(二)——Message、Handler、MessageQueue和Looper
  11. 正则验证,match()与test()函数的区别?
  12. 千万别在开发阶段用 uglify 插件了!(from Requirejs to Webpack)
  13. mysql按条件 导出sql
  14. Vue----常见面试题
  15. python多线程不能利用多核cpu,但有时候多线程确实比单线程快。
  16. QGraphicsItem的paint函数的一些相关问题
  17. 网盘资源分享:你不知道的JavaScript(上)
  18. Spark运行时错误与解决
  19. Go的微服务库kite
  20. CSS知识点(三)

热门文章

  1. smarty缓存控制
  2. 28Spring_的事务管理_银行转账业务加上事务控制_基于注解进行声明式事务管理
  3. user database的initial size和dbcc shrinkfile
  4. C# 与 Unity 同名函数
  5. js学习第二篇简单语法
  6. 20135220谈愈敏Blog6_进程的描述和创建
  7. Arduino智能小车制作报告
  8. 启动页面设置,icon图标设置
  9. 如何优雅的写一篇安利文-以Sugar ORM为例
  10. [BZOJ 1295][SCOI2009]最长距离(SPFA+暴力)