0

我无法通过 PHP 连接到 wsdl 服务。麻烦似乎来自身份验证。到目前为止,这是我的代码:

$soapURL = "http://myurl?wsdl";
    $soapLogin = Array(
        'User ID'=>'myusername', 
        'Password'=>"mypassword"
        );
$soapClient = new SoapClient($soapURL, $soapLogin);
$soapResult = $soapClient->mySoapFunction();

我有客户端定义的用户名和密码,我可以看到 wsdl 生成的 XML 文件,但是我无法辨别我是否正确引用了用户名和密码。尝试连接时出现以下错误:

Fatal error: Uncaught SoapFault exception: [a:InvalidSecurity] An error occurred when verifying security for the message. in E:\xampp\htdocs\TRMSosf\index.php:15 Stack trace: #0 E:\xampp\htdocs\TRMSosf\index.php(15): SoapClient->__call('GetWeekEvents', Array) #1 E:\xampp\htdocs\TRMSosf\index.php(15): SoapClient->GetWeekEvents('10', '20130527') #2 {main} thrown in E:\xampp\htdocs\TRMSosf\index.php on line 15

我是使用 SOAP 的新手,对 PHP 也很陌生,任何帮助都将不胜感激。

4

1 回答 1

0

我在这里的另一篇文章中找到了答案: Can I get Code Samples working with php and wsse

它涉及一个处理 WSSE 身份验证的类

class WSSESoapClient extends SoapClient {                                                                                           
protected $wsseUser;
protected $wssePassword;

public function setWSSECredentials($user, $password) {
$this->wsseUser = $user;
$this->wssePassword = $password;
}

public function __doRequest($request, $location, $action, $version, $one_way = 0) {
if (!$this->wsseUser or !$this->wssePassword) {

    return parent::__doRequest($request, $location, $action, $version, $one_way = 0);
}

// get SOAP message into DOM
$dom = new DOMDocument();
$dom->loadXML($request);
$xp = new DOMXPath($dom);
$xp->registerNamespace('SOAP-ENV', 'http://schemas.xmlsoap.org/soap/envelope/');

// search for SOAP header, create one if not found
$header = $xp->query('/SOAP-ENV:Envelope/SOAP-ENV:Header')->item(0);
if (!$header) {
    $header = $dom->createElementNS('http://schemas.xmlsoap.org/soap/envelope/', 'SOAP-ENV:Header');
    $envelope = $xp->query('/SOAP-ENV:Envelope')->item(0);
    $envelope->insertBefore($header, $xp->query('/SOAP-ENV:Envelope/SOAP-ENV:Body')->item(0));
}

// add WSSE header
$usernameToken = $dom->createElementNS('http://schemas.xmlsoap.org/ws/2002/07/secext', 'wsse:UsernameToken');
$username = $dom->createElementNS('http://schemas.xmlsoap.org/ws/2002/07/secext', 'wsse:Username', $this->wsseUser);
$password = $dom->createElementNS('http://schemas.xmlsoap.org/ws/2002/07/secext', 'wsse:Password', $this->wssePassword);
$usernameToken->appendChild($username);
$usernameToken->appendChild($password);
$header->appendChild($usernameToken);

// perform SOAP call
$request = $dom->saveXML();

return parent::__doRequest($request, $location, $action, $version, $one_way = 0);

}

于 2013-05-28T14:34:05.713 回答