Home > Article > Backend Development > How to Connect to a WS-Security Protected Web Service Using PHP?
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.
To establish a connection to a web service protected by WS-Security, utilize SoapHeader to create a WS-Security-compliant authentication header.
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]]);
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!