how can i post JSON object through cURL in php -


How can I post JSON objects in a web service, via cURL in php?

I have an array

$ data = array ('username' = & gt; 'abc', 'password' = & gt; 'pass');

The JSON object that I accept calls to call it, if I convert $ data to JSON with json_encode, it does not work for me.

  $ data = json_encode ($ data); Curl_setopt ($ CH, CURLOPT_POST, 1); Curl_setopt ($ CH, CURLOPT_POSTFIELDS, $ data);  

Am I doing something wrong? Or do I need more parameters to set up?

Thanks for the help in advance.

Tannem

Add:

  curl_setopt ($ Ch, CURLOPT_HTTPHEADER, array ('content-type: application / jason'));  

To specify that the data is JSON the default is application / x-www-form-urlencoded .


Comments