为什么Soap API标头身份验证不适用于我的网站

时间:2019-06-19 10:26:38

标签: php soap api-design

我在网站上设置了soap API。并添加身份验证标头以提供API的安全性。

在我的网站上工作,如果用户名/密码无效。它不显示错误消息。

我检查了代码,但找不到我的代码错误。

如果有人提到那个错误。

我的编码如下,

SERVER.PHP

<?php
// turn off WSDL caching
ini_set("soap.wsdl_cache_enabled","0");

// model, which uses in web service functions as parameter
class Server
{
    public static function authendicate($header_params){
        if($header_params->username == "user" && $header_params->password == "pass") {
            return true;
        } else {exit;
            throw new SOAPFault('Username or password is wrong',401);
        }
    }
}

CLIENT.PHP

class client {

    public function __construct() {
        $params = array(
            'location' => 'http://localhost/soapapi/server.php',
            'uri' => 'http://localhost/soapapi/server.php',
            'trace' => 1
        );

        $this->instance = new SoapClient(null, $params);
        // $this->instance = new SoapClient('http://localhost/soapapi/server.php?wsdl', ['trace'=> 1, 'cache_wsdl' => WSDL_CACHE_NONE]);

        $auth_params = new stdClass();
        $auth_params->username = "user";
        $auth_params->password = "passs";

        $header_params = new SoapVar($auth_params, SOAP_ENC_OBJECT);
        $header = new SoapHeader('codev','authendicate',$header_params,false);
        $this->instance->__setSoapHeaders(array($header));
    }

    public function getInfo($request_param) {
        return $this->instance->__soapCall('studentRec',$request_param);
    }

}
$client = new client;

0 个答案:

没有答案