首先使用SoapClient进行基础调用,若需WS-Security则手动构造包含UsernameToken的安全头并注入请求,或通过扩展类如NTLMSoapClient支持NTLM认证,最终发送带安全头的SOAP请求以完成身份验证与数据交互。

如果您尝试通过PHP调用一个需要身份验证的SOAP服务,但请求被拒绝或返回空响应,则可能是由于缺少WS-Security头信息或SOAP配置不正确。以下是实现PHP调用支持WS-Security的SOAP服务的具体步骤:
一、使用SoapClient进行基础SOAP调用
在PHP中,内置的SoapClient类可用于发起基本的SOAP请求。此方法适用于不需要复杂安全头的Web服务。
1、定义WSDL地址并创建SoapClient实例:
$client = new SoapClient('http://example.com/service.wsdl');
立即学习“PHP免费学习笔记(深入)”;
2、调用远程方法并传入参数:
$result = $client->GetUserInfo(['id' => 123]);
3、输出结果以检查返回数据:
var_dump($result);
二、手动构造带WS-Security头的SOAP请求
当目标服务要求WS-Security认证时,PHP原生SoapClient无法直接添加安全头,需通过自定义XML请求体实现。
1、构建包含UsernameToken的身份验证头:
$username = 'your_username';
$password = 'your_password';
$timestamp = gmdate('Y-m-d\TH:i:s\Z');
2、拼接完整的SOAP信封,包含wsse:Security头:
$soapHeader = '
3、将安全头条目注入SoapClient上下文选项中:
$options = [ 'trace' => 1, 'exceptions' => 1, 'stream_context' => stream_context_create([ 'http' => [ 'header' => "Content-Type: text/xml; charset=utf-8\r\n" . "SOAPAction: \"\"\r\n" ] ]) ];
4、发送自定义请求体:
$client->__doRequest($requestBody, $endpoint, '', '1.2', SOAP_1_2);
三、使用第三方库(如NTLMSoapClient)处理高级认证
对于集成Windows认证或NTLM机制的服务端点,可通过扩展SoapClient类来支持额外的身份验证流程。
1、安装支持NTLM的库,例如通过Composer引入:
composer require phpntlm/phpntlm
2、继承SoapClient并重写__doRequest方法以插入认证头:
class NTLMSoapClient extends SoapClient { private $user; private $pass; public function __construct($wsdl, $user, $pass, $options = []) { $this->user = $user; $this->pass = $pass; parent::__construct($wsdl, $options); } public function __doRequest($request, $location, $action, $version, $one_way = 0) { $headers = [ 'Method: POST', 'Connection: Keep-Alive', 'User-Agent: PHP-SOAP', 'Content-Type: text/xml; charset=utf-8', 'Authorization: NTLM ' . base64_encode("{$this->user}:{$this->pass}") ]; stream_context_set_option($this->__getSoapHeaders(), 'http', 'header', implode("\r\n", $headers)); return parent::__doRequest($request, $location, $action, $version, $one_way); } }
3、实例化NTLMSoapClient并调用服务方法:
$client = new NTLMSoapClient('http://example.com/service.wsdl', 'domain\\user', 'password');
$response = $client->SomeSecureMethod(['param' => 'value']);











