簡體   English   中英

使用PHP進行身份驗證以使用wsdl

[英]Authentication using PHP to consume wsdl

我無法通過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還是很新的,對您的幫助非常感謝。

我在這里的另一篇文章中找到了答案: 我可以使用php和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);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM