1

this is my parameter i want to pass in CURLOPT_POSTFIELDS

{"user":{"user_id":"346"}}    

example :

curl_setopt($ch, CURLOPT_POSTFIELDS, {"user":{"user_id":"346"}});

how i can do it ?

thanks

B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • 1
    By handing it over as valid string (which appears to be json encoded): `curl_setopt($ch, CURLOPT_POSTFIELDS, '{"user":{"user_id":"346"}}');` – arkascha Sep 10 '17 at 07:01
  • 2
    Possible duplicate of [curl POST format for CURLOPT\_POSTFIELDS](https://stackoverflow.com/questions/5224790/curl-post-format-for-curlopt-postfields) – rndus2r Sep 10 '17 at 07:05

1 Answers1

1

As per your example your data is json so You need to pass data as json string

$data_string = '{"user":{"user_id":"346"}}';
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);

Also when you pass json, set header as json application

curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);  
B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • don't set the Content-Length header manually, curl will do it for you automatically. and unlike you, curl won't make any typos – hanshenrik Sep 10 '17 at 09:58
  • btw, if you'd like to encode it from native php data, it's `json_encode(array('user'=>array('user_id'=>346)))` – hanshenrik Sep 10 '17 at 10:02