本文以某公司iphone 6手机预约接口开发为例,介绍php5下soap调用的实现过程。
一、基础概念 soap(simple object access protocol )简单对象访问协议是在分散或分布式的环境中交换信息的简单的协议,是一个基于xml的协议,它包括四个部分:soap封装(envelop),封装定义了一个描述消息中的内容是什么,是谁发送的,谁应当接受并处理它以及如何处理它们的框架;soap编码规则(encoding rules),用于表示应用程序需要使用的数据类型的实例; soap rpc表示(rpc representation),表示远程过程调用和应答的协定;soap绑定(binding),使用底层协议交换信息。
wsdl(web service description language)就是描述xml web服务的标准xml格式,wsdl由ariba、intel、ibm和微软等开发商提出。它用一种和具体语言无关的抽象方式定义了给定web服务收发的有关操作和消息。就其定义来说,你还不能把wsdl当作一种对象接口定义语言,例如,corba或com等应用程序体系结构就会用到对象接口定义语言。 wsdl保持协议中立,但它确实内建了绑定soap的支持,从而同soap建立了不可分割的联系。所以,当我在这篇文章中讨论wsdl的时候,我会假定你把soap作为了你的通讯协议。
soap和wsdl虽然是web service的两大标准,但是两者并没有必然的联系,都可以独立使用。它们之间的关系就类似http和html之间的关系。前者是一种协议,后者是对一个web server的描述。
二、php5下的配置 在php的的配置文件php.ini中,找到
extension=php_soap.dll
然后将前面的;号去掉,然后重启web服务
三、查询web service方法与参数、数据类型 某省电信公司的入单接口为http://***.ct10000.com/services/acceptedbusiness?wsdl
我们使用soapclient的__geunctions()和__gettypes()方法查看该接口的方法,参数和数据类型
只有__getfunctions中列出的接口才能被soap调用。
在根目录下创建代码soap.php
__getfunctions()); print_r($client->__gettypes()); } catch (soapfault $e) { print $e;}?>
在浏览器运行:http://localhost/soap.php后,返回结果如下
array
(
[0] => arrayof_xsd_anytype introduceacceptedbusiness(string $c3, string $c4, string $linkman, string $linknum, string $num, string $idcard, string $remark, string $address)
[1] => arrayof_xsd_anytype introduceacceptedbusinessbyaizhuangwei(string $subname, string $linkphone, string $idcard, string $address, string $businesstype, string $marketcode, string $surveycode, string $commanager, string $commanagerphone, string $bendiwang, string $fenju, string $zhiju, string $remark)
[2] => string introduceacceptedbusinessbystandardinterface(string $xmlstr)
[3] => string introduceacceptedbusinessbycallout(string $xmlstr)
[4] => string introduceacceptedbusinessbyyddj(string $xmlparam)
[5] => arrayof_xsd_anytype queryacceptedbusinessbyaizhuangwei(string $surveycode, string $starttime, string $endtime)
[6] => string querycalloutorderbyconfig(string $xmlparam)
)
array
(
[0] => anytype arrayof_xsd_anytype[]
)
其中有个方法 introduceacceptedbusinessbystandardinterface(string $xmlstr),将是开发文档中提到的要使用的接口,参数为xml字符串
另外有的接口中提到有soapheader认证,这就需要加入__setsoapheaders方法,具体可查看http://php.net/manual/zh/soapclient.setsoapheaders.php
四、提交入单 这一步就是需要根据开发文档拼接xml字符串,然后作为introduceacceptedbusinessbystandardinterface的参数传入
创建acceptedbusiness.php,内容如下
introduceacceptedbusinessbystandardinterface($xml); print_r($return);} catch (soapfault $e) { print_r('exception:'.$e);}?>
在浏览器中执行后,返回
0入单成功!2014100905523549742