首页 > 解决方案 > 如何使用 Message Security 将 PHP 代码与 WCF Web 服务集成

问题描述

再会。

我想通过 PHP 的 Message Security 与 WCF Web 服务集成。Web 服务需要来自客户端的证书(PHP 代码),并且不需要任何用户名或密码。它还使用 wsHttpBinding。我有一台运行 IIS7 的 Windows x64 电脑,我可以从我的浏览器执行 php 代码。

我尝试在 PHP 中使用 SoapClient() 和以下代码:

<?php

ini_set("soap.wsdl_cache_enabled", "0");

try
{
    $context = stream_context_create(["ssl" => ["local_cert" => "./cert/TestClient.pem", "CN_Name => CN=TestClient"]]);
    $options = ["soap_version" => SOAP_1_2, "authentication" => SOAP_AUTHENTICATION_DIGEST, "context" => $context];
    $service = new SoapClient("http://vanzyld:49321/pointerws/?wsdl", $options);

    $service->GetVehicleList([3538]);
}
catch (SOAPFault $f) 
{
    error_log('ERROR => '.$f);
}

?>

但,

对于 SOAP_1_2,这是调用返回的错误:

ERROR => SoapFault exception: [HTTP] Error Fetching http headers in C:\inetpub\phproot\WebService\GetVehicles.php:16
Stack trace:
#0 [internal function]: SoapClient->__doRequest('<?xml version="...', 'http://vanzyld:...', 'http://pointers...', 2, 0)
#1 C:\inetpub\phproot\WebService\GetVehicles.php(16): SoapClient->__call('GetVehicleList', Array)
#2 {main}

使用 SOAP_1_1,返回这个。

ERROR => SoapFault exception: [HTTP] Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'. in C:\inetpub\phproot\WebService\GetVehicles.php:16
Stack trace:
#0 [internal function]: SoapClient->__doRequest('<?xml version="...', 'http://vanzyld:...', 'http://pointers...', 1, 0)
#1 C:\inetpub\phproot\WebService\GetVehicles.php(16): SoapClient->__call('GetVehicleList', Array)
#2 {main}

经过大量研究,SoapClient 似乎无法很好地处理对实现 Message Security 的 WCF Web 服务的调用。其中一个建议是(从这里:如何安全地在 PHP 和 WCF 之间进行通信)我从 WSO2 安装了第 3 方包,但这似乎也不是最好的方法,因为 WSO2 不再支持这一点。即使按照有关如何在我的 PC 上安装 wsf 的所有步骤进行操作,我也无法在我的 PC 上安装 WSF。(https://wso2.com/project/wsf/php/2.0.0/docs/install_guide.html - 我按照步骤 2.1 和 2.4 进行操作) WSF 部分从未在我的 phpinfo 页面上弹出,因为它已启用。安装 WSF 后返回的 PHP 错误只是代码找不到 WSClient() 类。

是否有任何其他 PHP 类或库或第三方对象可用于成功地将 PHP 代码与使用 Message Security 的 WCF Web 服务集成?或者有没有办法让 SoapClient() 实际使用这种 WCF 服务?还是 PHP 在与 Message Security WCF Web 服务集成的能力方面受到限制?

谢谢你。达琳

标签: phpwindowswcfwcf-security

解决方案


在这两种情况下,您的标题似乎都有错误。SOAP_1_2 错误明确告诉您这一点,SOAP_1_1 错误抱怨设置了错误的内容类型,这意味着实际上没有正确发送内容类型标头。

尝试使用SoapHeader该类来修复您的代码:

http://php.net/manual/en/class.soapheader.php


推荐阅读