編碼的世界 / 優質文選 / 文明

php soap 使用實例


2022年5月25日
-   

SOAP 是基於XML和HTTP通訊協議,XML各個平台,各種語言都支持的一種語言。
WSDL 是網絡服務描述語言(Web Services Description Language),是一種使用XML格式的文檔。這種文檔可描述某個Web Service。可規定服務的位置,及服務提供的操作。
不同語言之間需要通信(例如: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模式靈活,以後再介紹這種模式的使用)
SOAP中主要用到三個類,SOAPServer,SOAPClient,SOAPFault
NO-WSDL模式: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.
";
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.
";
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來獲得我們設置的頭部的信息。
源碼下載地址:點擊查看

熱門文章