i漂泊

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 4193|回复: 0

PHP5下SOAP调用实现过程

[复制链接]
TA的礼物信息
  • 收到:0
  • 送出:2
发表于 2014-10-16 08:35:32 | 显示全部楼层 |阅读模式

本文以某公司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中,找到
  1. extension=php_soap.dll
复制代码
然后将前面的;号去掉,然后重启web服务

三、查询web service方法与参数、数据类型某省电信公司的入单接口为http://***.ct10000.com/services/AcceptedBusiness?wsdl
我们使用SoapClient的__geunctions()和__getTypes()方法查看该接口的方法,参数和数据类型
只有__getFunctions中列出的接口才能被soap调用。
在根目录下创建代码soap.php
  1. <?php
  2. header("content-type:text/html;charset=utf-8");
  3. try {
  4.         $client = new SoapClient("http://***.ct10000.com/services/AcceptedBusiness?wsdl");
  5.         print_r($client->__getFunctions());
  6.         print_r($client->__getTypes());  
  7. } catch (SOAPFault $e) {
  8.         print $e;
  9. }
  10. ?>
复制代码
在浏览器运行: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,内容如下
  1. <?php
  2. header("content-type:text/html;charset=utf-8");
  3. try {
  4.     $client = new SoapClient('http://***.ct10000.com/services/AcceptedBusiness?wsdl');
  5.         $xml = "
  6.     <?xml version='1.0' encoding='UTF-8' ?>
  7.     <PACKAGE>
  8.       <C3>**电信</C3>
  9.       <C4></C4>
  10.       <LINKMAN>张三</LINKMAN>
  11.       <LINKNUM>13412341234</LINKNUM>
  12.       <LINKADDRESS>广东深圳</LINKADDRESS>
  13.       <REMARK>iPhone 6</REMARK>
  14.       <CHANNEL></CHANNEL>
  15.       <GRIDCODE>1111111111111111111111111111111</GRIDCODE>
  16.       <AGENTCODE>2111</AGENTCODE>
  17.           <KEY>1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111</KEY>
  18.     </PACKAGE>
  19.   ";
  20.     $return = $client->introduceAcceptedBusinessByStandardInterface($xml);
  21.     print_r($return);
  22. } catch (SOAPFault $e) {
  23.     print_r('Exception:'.$e);
  24. }
  25. ?>
复制代码
在浏览器中执行后,返回
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <PACKAGE>
  3.         <STATUS>0</STATUS>
  4.         <REASON>入单成功!</REASON>
  5.         <ORDERSEQ>2014100905523549742</ORDERSEQ>
  6. </PACKAGE>
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|IPiaoBo Inc. ( 渝ICP备17002826号 )

GMT+8, 2024-5-5 11:04 , Processed in 0.071328 second(s), 46 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表