php webservice 验证,PHP WebService客户端验证

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

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

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.

$Service = new SOAP_Service_Secure('My_Service');

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

$Server->setObject($Service);

$Server->handle();

?>

3). Implementing a client:

$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,有着关系?望高手指点

应该是在 SOAP_Service_Secure 中找不到 someMethod 方法时,就会调用 __call 方法.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值