本文以某公司iPhone 6手機預定接口開發爲例,介紹PHP5下SOAP調用的實現過程。php
1、基礎概念
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的描寫敘述。html
2、PHP5下的配置
在php的的配置文件php.ini中,找到
extension=php_soap.dll
而後將前面的;號去掉,而後從新啓動web服務
3、查詢web service方法與參數、數據類型
某省電信公司的入單接口爲http://***.ct10000.com/services/AcceptedBusiness?wsdl
咱們使用SoapClient的__geunctions()和__getTypes()方法查看該接口的方法,參數和數據類型
僅僅有__getFunctions中列出的接口才幹被soap調用。
在根文件夾下建立代碼soap.php
<?php
header("content-type:text/html;charset=utf-8");
try {
$client = new SoapClient("http://***.ct10000.com/services/AcceptedBusiness?wsdl");
print_r($client->__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
4、提交入單
這一步就是需要依據開發文檔拼接xml字符串,而後做爲introduceAcceptedBusinessByStandardInterface的參數傳入
建立acceptedbusiness.php,內容例如如下
<?php
header("content-type:text/html;charset=utf-8");
try {
$client = new SoapClient('http://***.ct10000.com/services/AcceptedBusiness?wsdl');
$xml = "
<?web
xml version='1.0' encoding='UTF-8' ?>
<PACKAGE>
<C3>**電信</C3>
<C4></C4>
<LINKMAN>張三</LINKMAN>
<LINKNUM>13412341234</LINKNUM>
<LINKADDRESS>廣東深圳</LINKADDRESS>
<REMARK>iPhone 6</REMARK>
<CHANNEL></CHANNEL>
<GRIDCODE>1111111111111111111111111111111</GRIDCODE>
<AGENTCODE>2111</AGENTCODE>
<KEY>1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111</KEY>
</PACKAGE>
";
$return = $client->introduceAcceptedBusinessByStandardInterface($xml);
print_r($return);
} catch (SOAPFault $e) {
print_r('Exception:'.$e);
}
?>瀏覽器
在瀏覽器中執行後,返回
<?xml version="1.0" encoding="UTF-8"?>
<PACKAGE>
<STATUS>0</STATUS>
<REASON>入單成功!</REASON>
<ORDERSEQ>2014100905523549742</ORDERSEQ>
</PACKAGE>
框架