Home >Backend Development >PHP Tutorial >How to Connect to a WS-Security Protected Web Service Using PHP?

How to Connect to a WS-Security Protected Web Service Using PHP?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-07 02:45:03744browse

How to Connect to a WS-Security Protected Web Service Using PHP?

Connecting to WS-Security Protected Web Service with PHP

Problem:

Connecting to a password-protected web service using PHP and authenticating before making a request proves to be a challenge. Neither defining the service as a SoapClient nor a SoapServer seems to connect to the remote site successfully.

Solution:

To establish a connection to a web service protected by WS-Security, utilize SoapHeader to create a WS-Security-compliant authentication header.

Implementation:

Extend the SoapHeader class by creating a custom class that defines the WS-Security header specifics.

class WsseAuthHeader extends SoapHeader {
  // ... (your code to generate the WS-Security header)
}

Instantiate the custom header class with the necessary credentials (username, password) and pass it to the SoapClient constructor.

$wsse_header = new WsseAuthHeader($username, $password);
$client = new SoapClient('service URL', ['__setSoapHeaders' => [$wsse_header]]);

Additional Notes:

  • For cases involving WS-Security with a nonce and timestamp, refer to another implementation in the answer section.
  • Ensure to compare the method with the one provided by Stack Overflow at the link specified in the answer.

The above is the detailed content of How to Connect to a WS-Security Protected Web Service Using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn