SOAP 是基於XML和HTTP通信協議,XML各個平臺,各類語言都支持的一種語言。php
WSDL 是網絡服務描述語言(Web Services Description Language),是一種使用XML格式的文檔。這種文檔可描述某個Web Service。可規定服務的位置,及服務提供的操做。java
不一樣語言之間須要通訊(例如:php,java,c),能夠經過SOAP,WSDL使不一樣操做系統,不一樣技術的編程語言互相通訊。編程
php soap 擴展安裝服務器
擴展位置在php安裝包的 ext/soap 目錄,安裝步驟:網絡
cd php-5.3.15/ext/soap phpize ./configure sudo make sudo make test安裝成功後在phpinfo能夠看到soap擴展
SOAP有兩種操做方式,NO-WSDL 與 WSDL。編程語言
NO-WSDL模式:使用參數來傳遞要使用的信息函數
WSDL模式: 使用WSDL文件名做爲參數,並從WSDL中提取服務所需的信息。(每次修改都須要修改client與server的wsdl文件,沒有NO-WSDL模式靈活,之後再介紹這種模式的使用)ui
SOAP中主要用到三個類,SOAPServer,SOAPClient,SOAPFaultthis
NO-WSDL模式:url
soapHandle.class.php 處理請求
<?php class soapHandle{ public function strtolink($url=''){ return sprintf('<a href="%s">%s</a>', $url, $url); } } ?>server.php soap服務端
<?php // 服務器驗證 if ($_SERVER['PHP_AUTH_USER']!='fdipzone' || $_SERVER['PHP_AUTH_PW']!='123456') { header('WWW-Authenticate: Basic realm="MyFramework Realm"'); header('HTTP/1.0 401 Unauthorized'); echo "You must enter a valid login ID and password to access this resource.\n"; exit; } require("soapHandle.class.php"); // 處理請求的class try{ $server = new SOAPServer(null, array('uri'=>'http://demo.fdipzone.com/soap/server.php')); $server->setClass('soapHandle'); //設置處理的class $server->handle(); }catch(SOAPFault $f){ echo $f->faultString; // 打印出錯信息 } ?>client.php soap客戶端
<?php try{ $client = new SOAPClient(null, array( 'location' => 'http://demo.fdipzone.com/soap/server.php', // 設置server路徑 'uri' => 'http://demo.fdipzone.com/soap/server.php', 'login' => 'fdipzone', // HTTP auth login 'password' => '123456' // HTTP auth password )); echo $client->strtolink('http://blog.csdn.net/fdipzone').'<br>'; // 直接調用server方法 echo $client->__soapCall('strtolink', array('http://blog.csdn.net/fdipzone')); // 間接調用server方法 }catch(SOAPFault $e){ echo $e->getMessage(); } ?>
Header驗證例子:
server.php
<?php // 服務器驗證 if ($_SERVER['PHP_AUTH_USER']!='fdipzone' || $_SERVER['PHP_AUTH_PW']!='123456') { header('WWW-Authenticate: Basic realm="NMG Terry"'); header('HTTP/1.0 401 Unauthorized'); echo "You must enter a valid login ID and password to access this resource.\n"; exit(); } require 'SOAPHandle.class.php'; $config = array( 'uri' => 'http://demo.fdipzone.com/soap/server.php' ); $oHandle = new SOAPHandle; // no wsdl mode try{ $server = new SOAPServer(null, $config); $server->setObject($oHandle); $server->handle(); }catch(SOAPFault $f){ echo $f->faultString; } ?>client.php
<?php $config = array( 'location' => 'http://demo.fdipzone.com/soap/server.php', 'uri' => 'http://demo.fdipzone.com/soap/server.php', 'login' => 'fdipzone', 'password' => '123456', 'trace' => true ); try{ $auth = array('fdipzone', '654321'); // no wsdl $client = new SOAPClient(null, $config); $header = new SOAPHeader('http://demo.fdipzone.com/soap/server.php', 'auth', $auth, false, SOAP_ACTOR_NEXT); $client->__setSoapHeaders(array($header)); $revstring = $client->revstring('123456'); $strtolink = $client->__soapCall('strtolink', array('http://blog.csdn.net/fdipzone', 'fdipzone blog', 1)); $uppcase = $client->__soapCall('uppcase', array('Hello World')); echo $revstring.'<br>'; echo $strtolink.'<br>'; echo $uppcase.'<br>'; }catch(SOAPFault $e){ echo $e->getMessage(); } ?>SOAPHandle.class.php
<?php class SOAPHandle{ // class start // header 驗證 public function auth($auth){ if($auth->string[0]!='fdipzone' || $auth->string[1]!='654321'){ throw new SOAPFault('Server', 'No Permission'); } } // 反轉字符串 public function revstring($str=''){ return strrev($str); } // 字符傳轉連接 public function strtolink($str='', $name='', $openwin=0){ $name = $name==''? $str : $name; $openwin_tag = $openwin==1? ' target="_blank" ' : ''; return sprintf('<a href="%s" %s>%s</a>', $str, $openwin_tag, $name); } // 字符串轉大寫 public function uppcase($str){ return strtoupper($str); } } // class end ?>
Must Understand
這個參數指明瞭, 是否服務端必需要響應SoapHeader, 若是這個參數爲真, 而服務端並不能識別響應的Header,則會引起一個Soap Fault(Header not understood)。
SOAP_ACTOR_NEXT
actor指明瞭SoapHeader要傳遞給誰, 被哪一個Service處理。
SOAP_ACTOR_NEXT的意思就是, 下一個接受到這個請求頭的Service。
在SoapServer的構造函數中, 咱們能夠指明一個Server的Actor, 好比:
<?php $config = array( 'uri' => 'http://demo.fdipzone.com/soap/server.php', 'actor' => 'myserver' ); $server = new SOAPServer(null, $config); ?>而後就能夠在Client的SoapHeader中, 經過設置actor是myserver, 來讓指定的Server來得到咱們設置的頭部的信息。
源碼下載地址:點擊查看