马丁传奇

导航

PHP基于SOAP实现webservice

    简单对象访问协议(SOAP)是一种轻量的、简单的、基于 XML 的协议,它被设计成在 WEB 上交换结构化的和固化的信息。 SOAP 可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议( HTTP),简单邮件传输协议(SMTP),多用途网际邮件扩充协议(MIME)。它还支持从消息系统到远程过程调用(RPC)等大量的应用程序。

    PHP有两个扩展可以实现web service,一个是NuSoap,一个是php官方的soap扩展,由于soap是官方的,所以我们这里以soap来实现web service。由于默认是没有打开soap扩展的,所以自己先看一下soap扩展有没有打开。

    在soap编写web service的过程中主要用到了SoapServer,SoapClient,SoapFault三个类:


一、SoapServer类
1.这个类可以用来提供Web services。SoapServer有两种操作模式:

  * WSDL 模式:构造器可以使用WSDL文件名作为参数,并从WSDL文件中提取服务所使用的信息(使用Zend Studio可生成wsdl文件)。
  * Non-WSDL 模式:使用参数来传递要使用的信息(本文就以此模式演示)。

  在WSDL模式中,服务实现了WSDL提供的接口;在non-WSDL模式中,参数被用来管理服务的行为。

2.$soapServer = new SoapServer( mixed $wsdl [, array $options ]);
  参数1: $wsdl    如果使用wsdl模式就给值为wsdl文件所在URI路径,否则如不使用wsdl模式就给值null,在第2个参数$options中给服务器传递对应值;
 
  参数2: $options 此参数为数组类型,可以设置如:版本、编码及URI等参数。
         如: array('soap_version' => SOAP_1_2, 'actor' =>'http://example.org/ts-tests/C', 'encoding'=>'utf8','location'=>'http://test-uri/url', 'uri' => 'http://test-uri/')
         通常给出uri参数即可,uri参数代表命名空间,客户端location必须提供,而服务端的location是选择性的,可以不提供;

  可用方法函数列表:
  1 __construct
     作用:创建 SoapServer 对象
     用法:__construct ( mixed wsdl [, array options] )
     参数:wsdl 文件地址,options soap_version,encoding,actor,classmap
     返回:对象

  2 addFunction
     作用:为客户端导出一个或多个函数
     用法:addFunction ( mixed functions )
     参数:functions 函数,一个或多个,全部 SOAP_FUNCTIONS_ALL
     返回:无

  3 getFunctions
     作用:获取全部函数
     用法:getFunctions ()
     参数:无
     返回:函数数组

  4 setClass
     作用:导出类中的全部函数
     用法:setClass ( string class_name [, mixed args [, mixed ...]] )
     参数:class_name 类名 args 参数
     返回:无

  5 setPersistence
     作用:允许保存在PHP之间的会话(SESSION)请求数据
     用法:setPersistence ( int mode )
     参数:mode SOAP_PERSISTENCE_SESSION SOAP_PERSISTENCE_REQUEST
     返回:无

  6 fault
     作用:出错处理方法
     用法:fault ( string code, string string [, string actor [, mixed details [, string name]]] )
     参数:code 错误代码 string 简短错误信息 actor 导致错误的字符串 details 错误详细信息
     返回:无

  7 handle ( [string soap_request] )
     作用:处理一个SOAP请求,调用必要的功能,并发送回一个响应。
     用法:handle ( [string soap_request] )
     参数:soap_request 请求
     返回:无

  在SoapServer类的众多方法中,有三个方法比较重要。它们是: SoapServer::setClass(),SoapServer::addFunction()和SoapServer::handle()。


3.两种模式的使用示例:
  WSDL 模式:     $soapServer = new SoapServer('http://example.com/xxx/someName.wsdl',array('uri' => 'http://test-uri/'));
  Non-WSDL 模式: $soapServer = new SoapServer(null,array('uri' => 'http://test-uri/'));

  SoapServer类方法:
  $soapServer>setClass('{你的类名}');          //注册class
  $soapServer>addFunction('{你的函数名}');     //添加自定义函数
  $soapServer>addFunction(SOAP_FUNCTIONS_ALL); //添加当前主机环境配置的所有PHP可用函数(主要应用于跨语言调用时,使对方语言也可使用PHP函数)
  $soapServer>handle();                        //处理一个SOAP请求,调用必要的功能,并发送回一个响应

  注:setClass() 与 addFunction() 不可同时使用,不知道为什么~~~~


二、SoapClient类
1.这个类也是有对应的两种操作形式:
  * WSDL 模式
  * Non-WSDL 模式

2.$soapClient = new SoapClient( mixed $wsdl [, array $options ]);
  参数1,解释同上,略.....
  参数1,通常给出location和uri参数即可,uri参数代表命名空间,客户端location必须提供,而服务端的location是选择性的,可以不提供;
         location代表SoapServer的php文件的URL位置;

  可用方法函数列表:
  1 __construct
     作用:创建 SoapClient 对象
     用法:__construct ( mixed wsdl [, array options] )
     参数:wsdl 文件地址 或 null,
     options
      a、soap_version soap版本,encoding 编码,compression 压缩,classmap
      b、http身份验证 :login , password
      c、代理服务:proxy_host, proxy_port, proxy_login and proxy_password
      d、证书验证:local_cert , passphrase
      e、wsdl 为null 时:location , uri
     返回:对象

  2 __call
     作用:调用函数
     用法:__call ( string function_name, array arguments [, array options [, array input_headers [, array output_headers]]] )
     参数:function_name,arguments
     返回:无

  3 __doRequest
     作用:在执行HTTP请求
     用法:__doRequest ( string request, string location, string action, int version [, int one_way] )
     参数:request XML的SOAP请求 location 请求地址 action ,version
     返回:字符串

  4 __getFunctions
     作用:获取全部方法
     用法:__getFunctions()
     参数:无
     返回:函数数组

  5 __setCookie
     作用:设置cookie
     用法:__setCookie ( string name [, string value] )
     参数:name cookie名称 value cookie值
     返回:无

  6 __getLastRequest
     作用:获取最后的请求
     用法:__getLastRequest ()
     参数:无
     返回:最后的请求

  7 __getLastRequestHeaders
     作用:获取最后的请求头部信息
     用法:__getLastRequestHeaders ()
     参数:无
     返回:最后的请求头部信息

  8 __getLastResponse
     作用:获取最后的回应
     用法:__getLastRequest ()
     参数:无
     返回:最后的请求回应

  9 __getLastResponseHeaders
     作用:获取最后的回应头部信息
     用法:__getLastResponseHeaders ()
     参数:无
     返回:最后的回应头部信息

3.两种模式的使用示例:
  WSDL 模式:     $soapClient = new SoapClient('http://example.com/xxx/someName.wsdl',array('encoding'=>'utf8'));
  Non-WSDL 模式: $soapClient = new SoapClient(null,array('location'=>'http://test-uri/url','uri' => 'http://test-uri/'));


三、SoapFault类是出错处理类

参考文档: http://blog.csdn.net/binyao02123202/article/details/5681445
             http://blog.csdn.net/binyao02123202/article/details/5681500

四、demo示例 

<?php
//server端
session_start();

$uri = '127.0.0.1';
$soapServer=new SoapServer(null,array('uri'=>$uri));
$soapServer->setClass('Persion'); //注册class
$soapServer->handle();            //发送应答,处理SOAP请求     

/**
 * 定义一个名称Persion的类
 * @param   无
 * @return  无
 * @author  martinzhang
 */
class Persion{
    public $name = '张三';

    public function __construct(){
        
    }

    /**
     * 获取名称
     * @param  无
     * @return 返回当前name
     * @author martinzhang
     */
    public function getName(){
        return  $this->name;
    }
    
    /**
     * 接收客户端传递来的数据,并写入服务器文件或数据库
     * @param  setname 客户端传递来的名字
     * @return int 写入文件的字节数
     * @author martinzhang
     */
    public function putName($putname){
        return file_put_contents('./namelist.txt',$putname,FILE_APPEND); //追加写入
    }

    /**
     * 透传PHP内置函数 - strlen()
     * @param  string 待检查的字符串
     * @return int 返回字符串长度
     * @author martinzhang
     */
    public function soapStrlen($string){
        return  strlen($string);
    }


}
<?php
//client端

header('Content-Type:text/html;charset=UTF-8');

$location = 'http://others.com/0_module/0/PHP/webservice/class_serverSoap.php';
$uri      = '127.0.0.1';
try{
    $soapClient = new SoapClient(null,array('location'=>$location,'uri'=>$uri));    
    echo $soapClient->getName();
    echo '<br />';
    echo $soapClient->putName("lisiwangwu\n");
    echo '<br />';
    echo $soapClient->soapStrlen('abcd');       //相当于使用了strlen()函数
    echo '<br />';
    
    
}catch(SoapFault $fault){
    echo 'Error:'.$fault->faultcode.',String:'.$fault->faultstring;

}

 

posted on 2013-12-04 18:42  马丁传奇  阅读(1561)  评论(0编辑  收藏  举报