【问题标题】:PHP calling WCF service using WSHttpBindingPHP 使用 WSHttpBinding 调用 WCF 服务
【发布时间】:2014-12-25 16:20:25
【问题描述】:

目前我正在开发使用 WSHttpBinding 的 WCF 服务。到目前为止,该服务适用于 .NET 应用程序。然而,当谈到在 PHP 中使用此服务时,它会抛出一个错误。错误是因为 PHP 将 null 作为参数发送到 WCF 服务。

服务合同如下:

[ServiceContract]
public interface IWebsite : IWcfSvc
{
    [OperationContract]
    [FaultContract(typeof(ServiceException))]
    ResponseResult LostPassword(RequestLostPassword request);
}

用于参数的数据协定如下所示:

[DataContract]
public class RequestLostPassword
{
    [DataMember(IsRequired = true)]
    public string Email { get; set; }

    [DataMember(IsRequired = true)]
    public string NewPassword { get; set; }

    [DataMember(IsRequired = true)]
    public string CardNumber { get; set; }

    [DataMember(IsRequired = true)]
    public DateTime RequestStart { get; set; }
}

由于我不是专家,我花了一段时间才让 PHP 代码工作,但我最终写了一个这样的脚本:

$parameters = array(
    'Email' => "user@test.com",
    'NewPassword' => "test",
    'CardNumber' => "1234567890",
    'RequestStart' => date('c')
);

$svc = 'Website';
$port = '10007';
$func = 'LostPassword';
$url = 'http://xxx.xxx.xxx.xxx:'.$port.'/'.$svc;

$client = @new SoapClient(
    $url."?wsdl", 
    array(
        'soap_version' => SOAP_1_2, 
        'encoding'=>'ISO-8859-1', 
        'exceptions' => true,
        'trace' => true,
        'connection_timeout' => 120
    )
);

$actionHeader[] = new SoapHeader(
    'http://www.w3.org/2005/08/addressing', 
    'Action', 
    'http://tempuri.org/I'.$svc.'/'.$func,
    true
);

$actionHeader[] = new SoapHeader(
    'http://www.w3.org/2005/08/addressing', 
    'To',
    $url,
    true
);

$client->__setSoapHeaders($actionHeader);
$result = $client->__soapCall($func, array('parameters' => $parameters));

我不明白为什么它没有将参数传递给 WCF 服务。我有另一个服务很好,虽然它不需要参数。有人可以解释为什么会这样吗?我是一个完整的 PHP 菜鸟,只是想让它作为开发网站的人的示例。

【问题讨论】:

    标签: php wcf wshttpbinding


    【解决方案1】:

    我们找到了答案! 下面这行代码:

    $result = $client->__soapCall($func, array('parameters' => $parameters));
    

    应改为:

    $result = $client->__soapCall($func, array('parameters' => array('request' => $parameters)));
    

    显然你需要告诉 PHP 你的参数嵌套在一个名为“request”的数组中,该数组嵌套在一个名为 parameters 的数组中,当你想调用一个带有 datacontract 作为请求对象的 WCF 服务时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-08
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多