简体   繁体   English

使用PHP进行身份验证以使用wsdl

[英]Authentication using PHP to consume wsdl

I'm having trouble connecting to a wsdl service through PHP. 我无法通过PHP连接到wsdl服务。 The trouble seems to come in authentication. 麻烦似乎来自身份验证。 Here is my code so far: 到目前为止,这是我的代码:

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

I have the username and password as defined by the client, and I can see the XML file that the wsdl generates but no I cannot discern if I am referencing the username and passwords correctly. 我具有客户机定义的用户名和密码,并且可以看到wsdl生成的XML文件,但是不可以,如果我正确引用了用户名和密码,则无法辨认。 When trying to connect I get the following error: 尝试连接时出现以下错误:

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

I'm new to using SOAP and fairly new to PHP, any help is appreciated. 我是使用SOAP的新手,对于PHP还是很新的,对您的帮助非常感谢。

I found the answer in another article here: Can I get Code Samples working with php and wsse 我在这里的另一篇文章中找到了答案: 我可以使用php和wsse获得代码示例吗?

it involved a class that handled the WSSE authentication 它涉及一个处理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);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM