PHP Webservice的发布与调用

1.  环境配置

配置php.ini,把php_soap.dll前面的分号去掉,

不然会报错

class soapserver not found

重启apache后通过phpinfo()查看

这样是表示环境已经支持soap的webservice了,后面的事情就是写代码了。

2.  webservice的发布

发布出来的*.wsdl文件,其实是一个xml格式的文件,生成这个文件可以通过第3方软件,如ZendStudio 就可以生成。

示例,我用以下代码生成:

<?php
class CTest
{
public function __construct()
{
}
/**
*
* @param string $oParams
* @return string
*/
public function Add($oParams)
{
$sParams = $oParams->oParams[l1] ;
$oParams = json_decode($sParams);
$a = $oParams->a;
$b = $oParams->b;
$c = $a+$b;
return array('AddResult'=>$c);
}
}
?>

生成的xml文件是:

<?xml version="1.0" encoding="UTF-8" ?>
<wsdl:definitions targetNamespace="http://tempuri.org/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="AddRequest">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="oParams" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="AddResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="AddResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="AddSoapIn">
<wsdl:part name="parameters" element="tns:AddRequest" />
</wsdl:message>
<wsdl:message name="AddSoapOut">
<wsdl:part name="parameters" element="tns:AddResponse" />
</wsdl:message>
<wsdl:portType name="CTestSoap">
<wsdl:operation name="Add">
<wsdl:input message="tns:AddSoapIn" />
<wsdl:output message="tns:AddSoapOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CTestSoap" type="tns:CTestSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="Add">
<soap:operation soapAction="http://tempuri.org/Add" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="CTestSoap12" type="tns:CTestSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="Add">
<soap12:operation soapAction="http://tempuri.org/Add" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="CTest">
<wsdl:port name="CTestSoap" binding="tns:CTestSoap">
<soap:address location="http://192.168.6.44:80/webservice/server/server.php?wsdl[l2] " />
</wsdl:port>
<wsdl:port name="CTestSoap12" binding="tns:CTestSoap12">
<soap12:address location="http://192.168.6.44:80/ webservice/server/server.php?wsdl[l3] " />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

生成xml文件后,就要通过一个服务发布出去,如:

<?php
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);
include_once('../class/test.class.php');[l4]
$server = new SoapServer('wscache/CTest.1.0.wsdl');
$server->setClass('CTest');
$server->handle();
?>

然后可以向其他人或者系统提供出发布的地址,如:

http://192.168.6.44/webservice/server/server.php?wsdl

3.  webservice的调用

通过上面提供的地址,写一个调用页面

<?php
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);
header("Content-Type: text/html;charset=utf-8");
$client = new SoapClient('http://192.168.6.44/webservice/server/server.php?wsdl');
$str = '{"a":1,"b":20}';
//调用方法一
$r = $client->Add(array('oParams'=>$str)); //数组
//调用方法二
//$pParams->oParams = $str;
//$r = $client->__call('Add',array($pParams));//这个得是对象
var_dump($r);
?>

PHP Webservice的发布与调用相关推荐

  1. Axis2 webservice入门--Webservice的发布与调用

    一.Webservice发布 参考 http://www.cnblogs.com/demingblog/p/3263576.html 二.webservice 调用 部分参考:http://www.c ...

  2. SAP 中 Webservice的发布和调用过程。

    20180620 新建程序测试Webservice 服务.REPORT YTEST_WEBSERVICE_N .DATA LS_CONSUMER TYPE REF TO ZCL_CO_ZWS_ZPPT ...

  3. 基于CXF框架的webservice接口发布与调用

    目录 前言 正文 一,开发接口服务端(soap风格),接收SAP系统推送过来的数据 二,调用SAP提供的webservice接口(soap风格) 三,调用SRM系统提供的rest接口 四,接口调试工具 ...

  4. Axis2学习第一篇:Axis2开发WebService之发布和调用

    一.关于Axis2 Axis2是目前比较流行的WebService引擎.适用于开发RPC风格的程序. 二.使用Axis2开发WebService 下载与安装 下载地址:http://axis.apac ...

  5. 【WebService笔记01】使用JWS实现WebService接口的发布和调用

    这篇文章,主要介绍如何使用JWS实现WebService接口的发布和调用. 目录 一.JWS实现WebService接口 1.1.JWS发布WebService接口 (1)编写接口 (2)编写实现类 ...

  6. WebService的编写与调用

    WebService的编写与调用 编写:启动VS08,选择模版.c#语言,默认有"Hello World"方法,自己编写的webservice要注意的是必须加上[Web Metho ...

  7. 系统开发系列 之MyEclipse创建WebService详细教程和调用教程(spring框架+maven+CXF框架)

    1 回顾 [系统开发系列 之MyEclipse创建WebService详细教程和调用教程]介绍了使用JWS实现WebService接口的发布和调用,主要涉及的点有: (1)MyEclipse点击Fil ...

  8. HTTP 调用 WebService、CXF 动态调用 WebService

    概述   Web Service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用 ...

  9. 用cxf发布和调用web service

    用cxf发布和调用web service http://cxf.apache.org/docs/jax-ws-configuration.html  官方API helloword地址 用CXF来做w ...

最新文章

  1. jsonp跨域实现单点登录,跨域传递用户信息以及保存cookie注意事项
  2. 关于java设计模式笔记
  3. php指定时间显示内容,织梦用php判断某个时间段显示和隐藏内容
  4. 在sql2005中加数据库时出现无法打开物理文件
  5. adb实时获取屏幕_实时数仓 | 你需要的是一款合适且强大的OLAP数据库(上)
  6. PyCharm入门教程——在编辑器中选择文本
  7. 三维球体换算到二维_三维制图讲义04 - 基础几何体
  8. HTML5+CSS3之字体的下载使用
  9. 微信小程序弹框种类汇总
  10. 【Linux】Ubuntu18.04深度学习环境配置+Google+TIM+搜狗输入法等安装方法
  11. vnc内网远程工具 vnc内网远程工具及如何使用
  12. python之jieba分词库
  13. 新近出现的恶意软件:Visal.B
  14. 7个强大实用网站,我收藏了这么久,希望对你有帮助!
  15. 《白帽子讲Web安全 》 随手记(一)
  16. 阿里云发短信错误SignatureDoesNotMatch
  17. Java环境变量配置超详细教程
  18. [已迁移]数据结构-霍夫曼编码
  19. 计算机专业考哪些证书含金量高?考到就能加薪吗
  20. CSDN怎么更换皮肤

热门文章

  1. 化工学python_化工计算与软件应用(第2版) PDF
  2. 学计算机视觉台式机,回顾2020,2020年最受欢迎的7种电脑视觉工具
  3. 卷积神经网络基础:(8)递归神经网络RNN
  4. Paper7:R-CNN
  5. 和12岁小同志搞创客开发:设计一款亮度可调节灯
  6. 新冠疫情下,毕业照怎么拍?10行Python代码搞定
  7. OpenCV(十五)边缘检测1 -- Sobel算子(一阶微分算子,X、Y方向边缘检测)
  8. apache安装_Apache+PHP 安装 ---windows
  9. C++中结构体与类的区别(struct与class的区别)
  10. 在CentOS 6.9 x86_64上开启nginx 1.12.2的stub_status模块(ngx_http_stub_status_module)监控