php下調用soap實現對接

來源:互聯網
上載者:User

標籤:div   soap   ace   ice   程式   rip   查詢   des   結構   

PHP5下SOAP調用實現過程

本文以某公司iPhone 6手機預約介面開發為例,介紹PHP5下SOAP調用的實現過程。


一、基礎概念

SOAP(Simple Object Access Protocol )簡易物件存取通訊協定 (SOAP)是在分散或分布式的環境中交換資訊的簡單的協議,是一個基於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://***.******.com/services/AcceptedBusiness?wsdl
我們使用SoapClient的__geunctions()和__getTypes()方法查看該介面的方法,參數和資料類型
只有__getFunctions中列出的介面才能被soap調用。
在根目錄下建立代碼soap.php

<?phpheader("content-type:text/html;charset=utf-8");try {    $client = new SoapClient("http://***.******.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


四、提交入單

這一步就是需要根據開發文檔拼接xml字串,然後作為introduceAcceptedBusinessByStandardInterface的參數傳入
建立acceptedbusiness.php,內容如下

<?phpheader("content-type:text/html;charset=utf-8");try {    $client = new SoapClient(‘http://***.*******.com/services/AcceptedBusiness?wsdl‘);    $xml = "    <?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>

php下調用soap實現對接

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.