php soap 使用例項
SOAP 是基於XML和HTTP通訊協議,XML各個平臺,各種語言都支援的一種語言。
WSDL 是網路服務描述語言(Web Services Description Language),是一種使用XML格式的文件。這種文件可描述某個Web Service。可規定服務的位置,及服務提供的操作。
不同語言之間需要通訊(例如:php,java,c),可以通過SOAP,WSDL使不同作業系統,不同技術的程式語言互相通訊。
php soap 擴充套件安裝
擴充套件位置在php安裝包的 ext/soap 目錄,安裝步驟:
安裝成功後在phpinfo可以看到soap擴充套件cd php-5.3.15/ext/soap phpize ./configure sudo make sudo make test
SOAP有兩種操作方式,NO-WSDL 與 WSDL。
NO-WSDL模式:使用引數來傳遞要使用的資訊
WSDL模式: 使用WSDL檔名作為引數,並從WSDL中提取服務所需的資訊。(每次修改都需要修改client與server的wsdl檔案,沒有NO-WSDL模式靈活,以後再介紹這種模式的使用)
SOAP中主要用到三個類,SOAPServer,SOAPClient,SOAPFault
NO-WSDL模式:
soapHandle.class.php 處理請求
server.php soap服務端<?php class soapHandle{ public function strtolink($url=''){ return sprintf('<a href="%s">%s</a>', $url, $url); } } ?>
client.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; // 打印出錯資訊 } ?>
<?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
?>
SOAPHeader 第四與第五個引數說明:
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來獲得我們設定的頭部的資訊。原始碼下載地址:點選檢視