当前位置:首页 > PHP教程 > PHP高级教程

重新封装zend_soap实现http连接安全认证的php代码

复制代码 代码如下:

<?php
class myframework_soap_server extends zend_soap_server {
protected $_login = '';
protected $_password = '';
public function __construct($wsdl = null, array $options = null) {
parent::__construct($wsdl,$options);
if(isset($options['login'])){
$this->_login=$options['login'];
$this->_password=$options['password'];
$this->_authenticate();
}
}
private function _authenticate(){
$this->setauthenticate($this->_login,$this->_password);
}
public function sethttplogin($login){
$this->_login=$login;
}
public function sethttppassword($password){
$this->_password=$password;
if(isset($this->_login)){
$this->_authenticate();
}
}
public function setauthenticate($login,$password){
if ($_server['php_auth_user']!=$login || $_server['php_auth_pw']!=$password) {
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;
}
}
}
?>

复制代码 代码如下:

<?php
class soap_server_test {
public $view = '';
public $params = '';
public $requestobj = '';
public $dbobj = '';
function __construct() {
$this->view = $globals['view'];
$this->params = $globals['params'];
$this->requestobj = $globals['requestobj'];
$this->dbobj = $globals['dbobj'];
}
function indexaction(){
if(isset($_get['wsdl'])) {
$autodiscover = new myframework_soap_autodiscover();
$autodiscover->setclass('model_service_soapclasssettest');
$autodiscover->handle();
exit;
} else {
//$options= array('encoding' => 'utf-8','login'=>'tangjian','password'=>'123456');
$options= array('encoding' => 'utf-8');
$soap = new myframework_soap_server("http://tj.myframework.com/default/soap_server_test/index?wsdl",$options);
$soap->sethttplogin('tangjian');
$soap->sethttppassword('123456');
$soap->setclass('model_service_soapclasssettest');
$soap->handle();
exit;
}
}
function clientaction() {
//$options= array('encoding' => 'utf-8','login'=>'tangjian','password'=>'123456',
// 'compression' =>soap_compression_gzip);
$options= array('encoding' => 'utf-8',
'compression' =>soap_compression_gzip);
$client = new myframework_soap_client('http://tj.myframework.com/default/soap_server_test/index?wsdl',$options);
$client->sethttplogin('tangjian');
$client->sethttppassword('123456');
$result=$client->getpass('tang',"man");
print_r($result);
}
}
?>

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!