SOAP 是基于XML和HTTP通讯协议,XML各个平台,各种语言都支持的一种语言。

WSDL 是网络服务描述语言(Web Services Description Language),是一种使用XML格式的文档。这种文档可描述某个Web Service。可规定服务的位置,及服务提供的操作。

不同语言之间需要通信(例如:PHPJava,c),可以通过SOAP,WSDL使不同操作系统,不同技术的编程语言互相通信。


php soap 扩展安装

扩展位置在php安装包的 ext/soap 目录,安装步骤:

[plain] view plain copy 在CODE上查看代码片派生到我的代码片
  1. cd php-5.3.15/ext/soap  
  2. phpize  
  3. ./configure  
  4. sudo make  
  5. 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 处理请求

  1. <?php  
  2.   
  3. class soapHandle{  
  4.   
  5.     public function strtolink($url=''){  
  6.         return sprintf('<a href="%s">%s</a>', $url, $url);  
  7.     }  
  8.   
  9. }  
  10.   
  11. ?>  

server.php soap服务端

  1. <?php  
  2.   
  3. // 服务器验证  
  4. if ($_SERVER['PHP_AUTH_USER']!='fdipzone' || $_SERVER['PHP_AUTH_PW']!='123456') {  
  5.       header('WWW-Authenticate: Basic realm="MyFramework Realm"');  
  6.       header('HTTP/1.0 401 Unauthorized');  
  7.       echo "You must enter a valid login ID and password to access this resource.\n";  
  8.       exit;  
  9. }  
  10.   
  11. require("soapHandle.class.php"); // 处理请求的class  
  12.   
  13. try{  
  14.     $server = new SOAPServer(null, array('uri'=>'http://demo.fdipzone.com/soap/server.php'));  
  15.     $server->setClass('soapHandle'); //设置处理的class  
  16.     $server->handle();  
  17. }catch(SOAPFault $f){  
  18.     echo $f->faultString; // 打印出错信息  
  19. }  
  20.   
  21. ?>  

client.php soap客户端

  1. <?php  
  2.   
  3. try{  
  4.     $client = new SOAPClient(null, array(  
  5.                         'location' => 'http://demo.fdipzone.com/soap/server.php', // 设置server路径  
  6.                         'uri' => 'http://demo.fdipzone.com/soap/server.php',  
  7.                         'login' => 'fdipzone', // HTTP auth login  
  8.                         'password' => '123456' // HTTP auth password  
  9.                     ));  
  10.   
  11.     echo $client->strtolink('http://blog.csdn.net/fdipzone').'<br>';               // 直接调用server方法  
  12.     echo $client->__soapCall('strtolink', array('http://blog.csdn.net/fdipzone')); // 间接调用server方法  
  13. }catch(SOAPFault $e){  
  14.     echo $e->getMessage();  
  15. }  
  16.   
  17. ?>  

Header验证例子:

server.php

  1. <?php  
  2.   
  3. // 服务器验证    
  4. if ($_SERVER['PHP_AUTH_USER']!='fdipzone' || $_SERVER['PHP_AUTH_PW']!='123456') {  
  5.     header('WWW-Authenticate: Basic realm="NMG Terry"');  
  6.     header('HTTP/1.0 401 Unauthorized');  
  7.     echo "You must enter a valid login ID and password to access this resource.\n";  
  8.     exit();  
  9. }  
  10.   
  11. require 'SOAPHandle.class.php';  
  12.   
  13. $config = array(  
  14.             'uri' => 'http://demo.fdipzone.com/soap/server.php'  
  15. );  
  16.   
  17. $oHandle = new SOAPHandle;  
  18.   
  19. // no wsdl mode  
  20. try{  
  21.   
  22.     $server = new SOAPServer(null, $config);  
  23.     $server->setObject($oHandle);  
  24.     $server->handle();  
  25.   
  26. }catch(SOAPFault $f){  
  27.   
  28.     echo $f->faultString;  
  29.   
  30. }  
  31.   
  32. ?>  

client.php

  1. <?php  
  2.   
  3. $config = array(  
  4.             'location' => 'http://demo.fdipzone.com/soap/server.php',  
  5.             'uri' => 'http://demo.fdipzone.com/soap/server.php',  
  6.             'login' => 'fdipzone',  
  7.             'password' => '123456',  
  8.             'trace' => true  
  9. );  
  10.   
  11. try{  
  12.   
  13.     $auth = array('fdipzone', '654321');  
  14.   
  15.     // no wsdl  
  16.     $client = new SOAPClient(null, $config);  
  17.     $header = new SOAPHeader('http://demo.fdipzone.com/soap/server.php', 'auth', $auth, false, SOAP_ACTOR_NEXT);  
  18.     $client->__setSoapHeaders(array($header));  
  19.   
  20.     $revstring = $client->revstring('123456');  
  21.     $strtolink = $client->__soapCall('strtolink', array('http://blog.csdn.net/fdipzone', 'fdipzone blog', 1));  
  22.     $uppcase = $client->__soapCall('uppcase', array('Hello World'));  
  23.   
  24.     echo $revstring.'<br>';  
  25.     echo $strtolink.'<br>';  
  26.     echo $uppcase.'<br>';  
  27.   
  28. }catch(SOAPFault $e){  
  29.     echo $e->getMessage();  
  30. }  
  31.   
  32. ?>  

SOAPHandle.class.php

  1. <?php  
  2.   
  3. class SOAPHandle{ // class start  
  4.   
  5.     // header 驗證  
  6.     public function auth($auth){  
  7.         if($auth->string[0]!='fdipzone' || $auth->string[1]!='654321'){  
  8.             throw new SOAPFault('Server', 'No Permission');  
  9.         }  
  10.     }  
  11.   
  12.     // 反轉字符串  
  13.     public function revstring($str=''){  
  14.         return strrev($str);  
  15.     }  
  16.   
  17.     // 字符傳轉連接  
  18.     public function strtolink($str='', $name='', $openwin=0){  
  19.         $name = $name==''? $str : $name;  
  20.         $openwin_tag = $openwin==1? ' target="_blank" ' : '';  
  21.         return sprintf('<a href="%s" %s>%s</a>', $str, $openwin_tag, $name);  
  22.     }  
  23.   
  24.     // 字符串轉大寫  
  25.     public function uppcase($str){  
  26.         return strtoupper($str);  
  27.     }  
  28.   
  29.   
  30. // class end  
  31.   
  32. ?>  


SOAPHeader 第四与第五个参数说明:

Must Understand

这个参数指明了, 是否服务端必须要响应SoapHeader, 如果这个参数为真, 而服务端并不能识别响应的Header,则会引发一个Soap Fault(Header not understood)。

SOAP_ACTOR_NEXT

actor指明了SoapHeader要传递给谁, 被哪个Service处理。

SOAP_ACTOR_NEXT的意思就是, 下一个接受到这个请求头的Service。

在SoapServer的构造函数中, 我们可以指明一个Server的Actor, 比如:

  1. <?php  
  2. $config = array(  
  3.             'uri' => 'http://demo.fdipzone.com/soap/server.php',  
  4.             'actor' => 'myserver'  
  5. );  
  6. $server = new SOAPServer(null, $config);  
  7. ?>  

然后就可以在Client的SoapHeader中, 通过设置actor是myserver, 来让指定的Server来获得我们设置的头部的信息。

posted on 2016-10-18 22:41  飘渺的悠远  阅读(1136)  评论(0编辑  收藏  举报