码迷,mamicode.com
首页 > Web开发 > 详细

PHP WebService客户端验证

时间:2014-11-13 16:55:13      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:style   http   io   ar   os   sp   for   on   cti   

Here‘s my solution to make SOAP-headers based authentication.

1). First of all we define the decorator class for our service class:

<?php

class SOAP_Service_Secure
{
    protected $class_name    = ‘‘;
    protected $authenticated = false;

    // -----

    public function __construct($class_name)
    {
        $this->class_name = $class_name;

    }

    public function AuthHeader($Header)
    {
        if($Header->username == ‘foo‘ && $Header->password == ‘bar‘)
            $this->authenticated = true;

    }

    public function __call($method_name, $arguments)
    {
        if(!method_exists($this->class_name, $method_name))
            throw new Exception(‘method not found‘);

        $this->checkAuth();

        return call_user_func_array(array($this->class_name, $method_name), $arguments);

    }

    // -----

    protected function checkAuth()
    {
        if(!$this->authenticated)
            HTML_Output::error(403);

    }

}
?>

2). Then we pass an instance of it to the SoapServer object.

<?php

    $Service = new SOAP_Service_Secure(‘My_Service‘);

    $Server = new SoapServer(‘my-service.wsdl‘);

    $Server->setObject($Service);

    $Server->handle();

?>

3). Implementing a client:

<?php

    $Client = new SoapClient(‘http://example.com/my-service.wsdl‘, array(
        ‘exceptions‘ => true
    ));
    
    // -----
    
    $AuthHeader = (object) array(‘username‘ => ‘foo‘, ‘password‘ => ‘bar‘);
    
    $Headers[] = new SoapHeader(‘http://example.com‘, ‘AuthHeader‘, $AuthHeader);
    
    $Client->__setSoapHeaders($Headers);
    
    // -----
    
    $Result = $Client->someMethod();

?>

非常不错!不过看的不是太明白,SOAP_Service_Secure->__call,好像跟 SosapClient->__call,有着关系?望高手指点

PHP WebService客户端验证

标签:style   http   io   ar   os   sp   for   on   cti   

原文地址:http://my.oschina.net/skq/blog/344010

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!