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

如果您尝试通过PHP调用一个需要身份验证的SOAP服务,但请求被拒绝或返回空响应,则可能是由于缺少WS-Security头信息或SOAP配置不正确。以下是实现PHP调用支持WS-Security的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认证时,PHP原生SoapClient无法直接添加安全头,需通过自定义XML请求体实现。
1、构建包含UsernameToken的身份验证头:
$username = 'your_username';
$password = 'your_password';
$timestamp = gmdate('Y-m-dTH:i:sZ');
2、拼接完整的SOAP信封,包含wsse:Security头:
$soapHeader = 'zuojiankuohaophpcnwsse:Security soap:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> <wsse:UsernameToken wsu:Id="UsernameToken-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> <wsse:Username>' . $username . '</wsse:Username> <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">' . $password . '</wsse:Password> <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">' . base64_encode(random_bytes(16)) . '</wsse:Nonce> <wsu:Created>' . $timestamp . '</wsu:Created> </wsse:UsernameToken> </wsse:Security>';
3、将安全头条目注入SoapClient上下文选项中:
$options = [ 'trace' => 1, 'exceptions' => 1, 'stream_context' => stream_context_create([ 'http' => [ 'header' => "Content-Type: text/xml; charset=utf-8 " . "SOAPAction: "" " ] ]) ];
4、发送自定义请求体:
$client->__doRequest($requestBody, $endpoint, '', '1.2', SOAP_1_2);
对于集成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(" ", $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']);
以上就是如何通过PHP调用SOAP服务_PHP SOAP服务调用与WS-Security实现教程的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号