PHP中soap的使用例子
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
最新文章
- bzoj4398:福慧双修
- Util应用程序框架公共操作类(十二):Lambda表达式公共操作类(三)
- React Native分析(index.ios.js)
- vs克隆新建团队项目
- 【POJ】【1067】取石子游戏
- 百练_4120 硬币(DP)
- Android imageview显示圆形图片
- DevExpress GridControl 列中显示图片
- 利用PCA来简化数据
- Android线程之异步消息处理机制(二)——Message、Handler、MessageQueue和Looper
- 正则验证,match()与test()函数的区别?
- 千万别在开发阶段用 uglify 插件了!(from Requirejs to Webpack)
- mysql按条件 导出sql
- Vue----常见面试题
- python多线程不能利用多核cpu,但有时候多线程确实比单线程快。
- QGraphicsItem的paint函数的一些相关问题
- 网盘资源分享:你不知道的JavaScript(上)
- Spark运行时错误与解决
- Go的微服务库kite
- CSS知识点(三)
热门文章
- smarty缓存控制
- 28Spring_的事务管理_银行转账业务加上事务控制_基于注解进行声明式事务管理
- user database的initial size和dbcc shrinkfile
- C# 与 Unity 同名函数
- js学习第二篇简单语法
- 20135220谈愈敏Blog6_进程的描述和创建
- Arduino智能小车制作报告
- 启动页面设置,icon图标设置
- 如何优雅的写一篇安利文-以Sugar ORM为例
- [BZOJ 1295][SCOI2009]最长距离(SPFA+暴力)