PHP中soap的用法实例
作者:shichen2014 发布时间:2023-11-14 09:40:24
本文实例讲述了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);
}
?>
希望本文所述对大家的PHP程序设计有所帮助。
猜你喜欢
- QQ通过返回不同的图片,来表示在线或离线,图标也随之变换,既然图片不同,那么,返回的HTTP头信息中的Content-Length 也一定不
- 于是我登陆主机的管理面板,重启了网站服务,ldb文件就消失了,网站打开也正常了,但过了数小时后,网站又再次出现以上情况,此种情况持续了好一段
- 你可能在使用MySQL过程中,各种意外导致数据库表的损坏,而且这些数据往往是最新的数据,通常不可能在备份数据中找到。本章将讲述如何检测MyS
- 一、xajax与其它ajax框架的比较 xajax功能很简单,但很灵活!~它不象其它一些大的框架,功能确实强大,但执行速度不敢恭维。。功能虽
- 最近几天仔细研究了一下vertical-align这个属性,结果让我大吃一惊,这个很“资深”的CSS标准竟然在各个浏览器里面的表现都各不相同
- 这段时间我一直在说设计需要有层次感,这种层次感可能有很多类型,比如色彩的层次感,或是元素的层次感。当一个设计缺乏层次感的时候页面所表现出来的
- Mysql默认是不可以通过远程机器访问的,通过下面的配置可以开启远程访问.我的Mysql环境是ubuntu+mysql51.修改/etc/m
- ASP实现即时显示当前页面浏览人数online.asp文件 <!--#include file="dbconn.a
- 1. 确认已经安装了NT/2000和SQL Server的最新补丁程序,不用说大家应该已经安装好了,但是我觉得最好还是在这里提醒一
- 原理:自定义javascript中的oncontextmenu事件,然后使用div层模拟菜单。知道了这个原理结合美工相信你可以做出很漂亮的自
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 13 - Regular ExpressionsM
- 代码如下: <% '屏蔽主流的下载工具 Dimxurl,xtool '获取浏览器AGENT xurl=lcase(Re
- 一个简单的例子:将如下代码另存为.wsc文件,并右键“注册”(卸载时右键“不注册”)。<Component> <regis
- 以前把守护进程与后台任务搞混了,后面看了文章才知道这两者的区别,写此文表达自己对守护进程的理解.1:什么是守护进程?所谓守护进程是一种是 L
- 我想把本篇作为css基础教程的序曲,从今天开始翻译和整理我从设计网页以来所学到的css基础知识。本教程会分成N个部分,单独发表,其间可能会插
- 工厂模式(Factory Pattern)是什么工厂模式是一种创建型模式,它提供了一种创建对象的最佳方式。在工厂模式中,我们在创建对象时不会
- 代码如下:<input name="file" type="file"> formsiz
- class ascii{function decode($str){ preg_match_all( &
- 今天运行程序时,在Oracle中输入SQL语句:select * from USERS as u ,程序报错输入select * from
- 一、给定一个日期值,求出此日期所在星期的星期一和星期天的日期数据 例如给定一个日期 2010-09-01,求出它所在星期的星期一是2010-