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来得到咱们设置的头部的信息。
源码下载地址:点击查看