php的webservice的soapheader认证问题

参数通过类传输:
class authentication_header {  
     private $username;  
     private $password;  
     public function __construct($username, $password) {  
         $this->username = $username;  
         $this->password = $password;  
     }  

服务端:
$wsdl_path = FCPATH.'wsdl/user.wsdl';
$s = new SoapServer($wsdl_path,array('actor' => 'user'));
$s -> setClass("user");
$s -> handle();

客户端:$wsdl_path = 'http://api.mypharma.com/wsdl/user.wsdl';
$authvalues = new authentication_header('liu','1234456');
$header = new SoapHeader('urn:Solsoft_user', 'Authentication', $authvalues, false, 'user');
$client = new SoapClient($wsdl_path,array('trace'=>1));
$client->__setSoapHeaders(array($header));
$a = $client->status(1);
红色是服务类的认证方法名称

服务类:
class user
{
    public $rootPath;
    public $Authenticated;
    public $username;
    public $password;
    function __construct()
    {
        $this->rootPath=dirname(__FILE__);
        require_once $this->rootPath.'/lib/db_class.php';
        require_once $this->rootPath.'/model/api_base_model.php';
    }

    public function Authentication($username,$password)
    {  
        $this->username =$username;
        $this->password =$password;
         if($this->username == 'liumeng' && $this->password == '123456'){
            $this->Authenticated = true;  
         } else {  
            $this->Authenticated = false;   
         }
    }
    
    public function status($id)
    {
        if($this->Authenticated){
            require_once $this->rootPath.'/model/member_model.php';
            $member_model =  new Member_Model();
            $a = $member_model->get_data_by_id($id);
            return json_encode($a);
        }else{
            return json_encode(array('error'=>'wrong username or password!'));
        }
    }
}
红色是传递参数的核心,我试了好长时间才正确,报了N次没有参数的错误,其实还可以通过获取所有输入参数分析得到。

原文地址:https://www.cnblogs.com/kudosharry/p/3262121.html