登录
首页 >  文章 >  php教程

PHP调用SOAP服务详解教程

时间:2025-12-23 20:54:03 359浏览 收藏

亲爱的编程学习爱好者,如果你点开了这篇文章,说明你对《PHP调用SOAP服务教程》很感兴趣。本篇文章就来给大家详细解析一下,主要介绍一下,希望所有认真读完的童鞋们,都有实质性的提高。

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

如何通过PHP调用SOAP服务_PHP SOAP服务调用与WS-Security实现教程

如果您尝试通过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');

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 = ' ' . $username . ' ' . $password . ' ' . base64_encode(random_bytes(16)) . ' ' . $timestamp . ' ';

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']);

今天带大家了解了的相关知识,希望对你有所帮助;关于文章的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>