PHP5下SOAP調用實現過程,php5soap調用過程
本文以某公司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 = " **電信 張三 13412341234 廣東深圳 iPhone 6 1111111111111111111111111111111 2111 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111 "; $return = $client->introduceAcceptedBusinessByStandardInterface($xml); print_r($return);} catch (SOAPFault $e) { print_r('Exception:'.$e);}?>
在瀏覽器中執行後,返回
xml version="1.0" encoding="UTF-8"?><PACKAGE> <STATUS>0STATUS> <REASON>入單成功!REASON> <ORDERSEQ>2014100905523549742ORDERSEQ>PACKAGE>
php webservice是什
soap 相比nusoap來說,優勢之一是用c開發並編譯成php內建函式庫,而NuSOAP 完全由PHP語言編寫,由一系列PHP 類組成。優勢之二,nusoap是很早以前就有的,從2005-07-27 之後就停止更新了,而Soap在php5版本新增,隨著php6對webservice的支援,我相信soap這個函數庫的地位肯定會不斷上升。
php5的Soap 函數庫使用起來很方便,wsdl可以使用zend Development Environment 開發工具產生。
注意幾點問題 :
1. 為了提高效率,php對 wsdl檔案提供了緩衝功能,開發的時候可以將使用ini_set("soap.wsdl_cache_enabled", 0); 讓其失效,因為開發過程經常要修改wsdl檔案;
2. SOAP(Simple Object Access Protocol) 簡易物件存取通訊協定 (SOAP),在php5不僅僅可以提供對象setClass給遠端存取調用,還可以提供方法addFunctions。所以SOAP中的 'O' 已經被擴充了 。
3. 服務端有可能取不到用戶端POST過來的資料,這可能是php5 soap functions的bugs;解決辦法在下文的服務端舉常式序中有一段代碼:
if (isset($HTTP_RAW_POST_DATA)) {
$request = $HTTP_RAW_POST_DATA;
} else {
$request = file_get_contents('php://input');
}
下面是舉常式序原始碼。
soap用戶端舉例:
ini_set("soap.wsdl_cache_enabled", 0);
try{
$soap = new SoapClient('authenticate/idolol.wsdl');
$soap->get_avatar(230);
$functions = $soap->__getFunctions();
print_r($functions);
$types = $soap->__getTypes();
print_r($types);
}catch(SoapFault $fault){
trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
}
?>
soap 服務端舉例:
require './soap_functions.php';
ini_set("soap.wsdl_cache_enabled", 0);
$server = new SoapServer('authenticate/idolol.wsdl',array('encoding'=>'UTF-8'));
$server->addFunction(array("user_login","se......餘下全文>>
php 調用webservice函數 不用SoapClient 還有什別的方法?
webservice有很多種,只有SOAP類型的webservice才使用SoapClient調用。
其實SoapClient是對soap請求的封裝。你也可以使用底層的介面來訪問,不過這需要你瞭解SOAP協議,而且非常麻煩。
有一個叫nusoap的庫,可以方便地編寫SOAP服務和SOAP請求,你可以瞭解一下。
望採納,謝謝支援!
http://www.bkjia.com/PHPjc/894769.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/894769.htmlTechArticlePHP5下SOAP調用實現過程,php5soap調用過程 本文以某公司iPhone 6手機預約介面開發為例,介紹PHP5下SOAP調用的實現過程。 一、基礎概念 SOAP(...