目前我正在研究使用 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菜鸟,只是想让它成为开发网站的人的一个例子。
我们找到了答案!下面的代码行:
$result = $client->__soapCall($func, array('parameters' => $parameters));
应改为:
$result = $client->__soapCall($func, array('parameters' => array('request' => $parameters)));
显然,当您想使用数据协定作为请求对象调用 WCF 服务时,您需要告诉 PHP 您的参数嵌套在一个名为 'request' 的数组中,该数组嵌套在名为参数的数组中。