致命错误:未捕获的SoapFault异常:[ns1:Client.AUTH_1]身份验证失败

时间:2014-02-28 13:34:26

标签: php soap

在我的wsdl文件中,我有一个用户身份验证块:

<!-- User authentication -->

    <element name="UserAuthentication">
        <complexType>
            <sequence>
                <element name="iId" type="xsd:int" />
                <element name="sPassword" type="xsd:string" />
                <element name="sType" type="api:UserType" />
            </sequence>
        </complexType>
    </element>

我正在尝试像这样实例化一个SOAP调用:

$client = new SoapClient("http://api.example.com/v2/example?wsdl",
    array(
         'iId' => 123456, 
         'sPassword' => 'fhfhfhfhfhfhfh46464dtdts64iyiyi', 
         'sType' => 'ghfh57477gghdkskdk68585jghhddhdghds'));

提供实际值。脚本报告:

SoapFault exception: [ns1:Client.AUTH_1] Authentication Failed in

我错过了什么?

2 个答案:

答案 0 :(得分:3)

您将请求数据传递给SoapClient选项,而不是肥皂调用的参数。

代码应如下所示(假设soap调用为Authenticate):

$client = new SoapClient("http://api.example.com/v2/example?wsdl");

$response = $client->Authenticate(Array(
   'iId' => 123456, 
   'sPassword' => 'fhfhfhfhfhfhfh46464dtdts64iyiyi', 
   'sType' => 'ghfh57477gghdkskdk68585jghhddhdghds'));
   ));

而且,无论如何,您应该使用try..catch来防止例外代码崩溃。

try
{
   // code here
}
catch(Exception $e)
{
   // error handling goes here
   die("Error: ". $e->getMessage()."\n");
}

答案 1 :(得分:1)

如果没有完整的WSDL规范,很难说出问题所在。但我的猜测是你正在看规格。错误。可能需要发送的是标题,而不是需要调用的实际方法。

所以试试这样的事情:

// SOAP client options
$options = array(
    'soap_version' => SOAP_1_2,
    'trace'        => true,
);

// initialise the SOAP client
$apiURL = 'http://api.example.com/v2/example?wsdl';
$namespace = 'http://api.example.com/v2/example?wsdl';

$client = new SoapClient($apiURL, $options);

// the SOAP headers
$headers = array();

$ua = array(
    'iId' => 123456,
    'sPassword' => 'fhfhfhfhfhfhfh46464dtdts64iyiyi', 
    'sType' => 'ghfh57477gghdkskdk68585jghhddhdghds',
);

$headers[] = new SoapHeader($namespace, 'UserAuthentication', $ua);

// we could add multiple headers if needed

$client->__setSoapHeaders($headers);

// call the API function
$response = $client->__soapCall("whateverYourTryingToCall", null);