zoukankan      html  css  js  c++  java
  • 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次没有参数的错误,其实还可以通过获取所有输入参数分析得到。

  • 相关阅读:
    JDBC的步骤
    Java异常
    两个init方法的区别
    迭代器、foreach循环、泛型集合
    servlet的生命周期
    集合类对比
    在servlet中的中文乱码,相对路径和绝对路径
    【转】学习使用Jmeter做压力测试(一)--压力测试基本概念
    【转】jmeter压力测试
    【转】配置Jmeter的自定义参数
  • 原文地址:https://www.cnblogs.com/kudosharry/p/3262121.html
Copyright © 2011-2022 走看看