开发者

how to send an object to C# WCF service from PHP using SOAP

开发者 https://www.devze.com 2023-01-08 17:37 出处:网络
I\'m having trouble sending a custom object thats defined as a datacontract in my WCF web service from PHP. I\'m attempting to accomplish this via SOAP.

I'm having trouble sending a custom object thats defined as a datacontract in my WCF web service from PHP. I'm attempting to accomplish this via SOAP.

Here is what the dataContract looks like:

[DataContract]
    public class simplyCustomer
    {
        [DataMember]
        public int id;
        [DataMember]
        public string name;
        [DataMember]
        public string contact;
        [DataMember]
        public string street1;
        [DataMember]
        public string street2;
        [DataMember]
        public string city;
        [DataMember]
        ...
    }

So I have a function that takes simplyCustomer as parameter on WCF service. The php can receive simplyCustomer just fine using another function that returns one. However, if I call the one that accepts it using this code in PHP:

$retVal = $simplyService->__soapCall("addCustomer",array('parameters'=>$params));

The SOAP envelope that this call generates leaves the object NULL causing the WCF service to complain about null reference.

Here is the envelope that is generated:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/"><SOAP-ENV:Body><ns1:addCustomer/></SOAP-ENV:Body></SOAP-ENV:Envelope>

The parameters should be where addCustomer is but there's nothing there.

Any help 开发者_如何学JAVAwould be greatly appreciated!

Thanks!


I faced the same issue when building a private project.

My solution was to use T4 to generate service- and datacontract proxies, maybe you find it useful. Message me if you have any questions, the templates are not tested to their full extent.

You find the templates on github, including sample servicecontracts/datacontracts:

https://github.com/schaermu/wcf-phpclient-t4

feel free to fork the project!

cheers


Though this is asked while before, thought to add my input since I came accros the same issue.

Assuming your operation contract look something similar to

[OperationContract]    
public <return_type> addCustomer(simplyCustomer parameters);

what you have suggested should work given that $params got all the required values set on initialisation where ever that has done and variable names are exactly similar to data contract.

Few tips going through this though.

1) If you have manage to send the object to the service, Could check the service logs to see what has gone wrong.

2) Just check your constructor to see all the parameters are set.

3) Also good to check that soap client is initialized and consumed properly.

eg : (following is, one of many possibilities)

$client = new Client( $baseUrl, array('soap_version' => SOAP_1_1,));
$result = $client->addCustomer(array("parameters" => $params ));


I had the same kind of problem. Cant see how you built your $params but I solved mine by doing this:

$username = "test";
$password = "test";

$input = array ("composite" => array("Password" => $password, "UserName" => $username));

$result=$client->__SoapCall('AuthenticateUser',array('parameters'=>$input));

My service method looks like this

[OperationContract]
AuthenticationData AuthenticateUser(AuthenticationData composite);

Using Fiddler is a good help when figuring out how to build your Soap stuff

0

精彩评论

暂无评论...
验证码 换一张
取 消