Search code examples
phpwcfwshttpbinding

PHP calling WCF service using WSHttpBinding


Currently I am working on WCF Service that is using WSHttpBinding. So far the service works great with .NET applications. However when it comes to using this service in PHP it throws me an error. The error is caused because PHP sends null as the parameter to the WCF services.

The service contract looks as follows:

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

The data contract that is used for the parameters looks like:

[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; }
}

Since I am not an expert, it took me a while to get the PHP code working, but I end up writing a script like this:

$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));

What I don't understand is why it is not passing the parameters to the WCF service. I have another service that works just fine although that does not require parameters. Can someone explain why this happens? I am a complete PHP noob and just want to get this to work as an example for the guy who is developing the website.


Solution

  • We found the answer! The line of code below:

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

    Should be changed to:

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

    Apparently you need to tell PHP that your parameters are nested in an array called 'request', which is nested in an array called parameters, when you want to call a WCF service with a datacontract as a request object.