Best way to CURL for WCF

I can send a message to a web service I wrote in C # from PHP via a CURL request, but I cannot get the request to go through correctly. No matter what I do, the only way I can get the message to show is if I have json_encode

the form data headers. So my PHP code ends like this:

$ user = array ('id' => 5, 'first_name' => 'First', 'last_name' => 'Last');

$ uch = curl_init ();
curl_setopt ($ uch, CURLOPT_URL, "http: // localhost: 50115 / Service1.svc / postUser");
curl_setopt ($ uch, CURLOPT_POST, 1);
curl_setopt ($ uch, CURLOPT_POSTFIELDS, json_encode ("user =". json_encode ($ user)));
curl_setopt ($ uch, CURLOPT_HTTPHEADER, array (
                                            'Content-Type: application / json'
                                            ));
curl_setopt ($ uch, CURLOPT_RETURNTRANSFER, true);
$ response = curl_exec ($ uch);

When I set the line:

curl_setopt($uch, CURLOPT_POSTFIELDS, json_encode("user=" . json_encode($user)));

      

in

curl_setopt($uch, CURLOPT_POSTFIELDS, array("user" => json_encode($user)));
--OR--
curl_setopt($uch, CURLOPT_POSTFIELDS, 
                    http_build_query(array("user" => json_encode($user))));
--OR--
curl_setopt($uch, CURLOPT_POSTFIELDS, json_encode($user));

      

there is no error in PHP, the C # service does not return an error (I have a parameter to return exceptions set in the config), and the function breakpoint never hits.

On the C # side, I have an interface and a service configured like this:

// Interface
[OperationContract]
[WebInvoke (Method = "POST",
            BodyStyle = WebMessageBodyStyle.Bare, // Any other value, the break-point 
                                                // will not trigger
            RequestFormat = WebMessageFormat.Json,
            UriTemplate = "postUser")]
void PostUser (string user);

// Service
public void PostUser (string user)
{
    var request = user;
    int i = 5; // Debug set here
}

When the breakpoint hits, I can see what user

matters:

user={"id":5,"first_name":"First","last_name":"Last"}

      

I would like to be meaningful user

simply:

{"id":5,"first_name":"First","last_name":"Last"}

      

Can you get such an answer? If so, what do I need to change to make it work?

+3


source to share


1 answer


After a few combinations in PHP and C #, I finally got it to work. In PHP, CURLOPT_POSTFIELDS

you need to install with:

curl_setopt($uch, CURLOPT_POSTFIELDS, json_encode(array("user" => json_encode($user))));

      



Then in C # I needed to change BodyStyle=WebMessageBodyStyle.Bare

to BodyStyle=WebMessageBodyStyle.Wrapped

;

+1


source







All Articles