php - How to use variables in a curl post? -


i creating php script use curl add new row in parse database. having difficulty adding variables setopt statement. if lead me in right direction, appreciate it.

this php code executing curl statment:

//curl commands send information parse   $ch = curl_init('https://api.parse.com/1/classes/classname');  curl_setopt($ch,curlopt_httpheader,     array('x-parse-application-id:secret1',     'x-parse-rest-api-key:secret2',     'content-type: application/json'));  curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_post, 1);   curl_setopt($ch, curlopt_postfields,  "{\"name\":\"$devicename\"}" );  echo curl_exec($ch); curl_close($ch); 

the response is:

{"code":107,"error":"invalid json"}

thank in advance!

is there reason can't encode array before sending data?

for example (untested):

$arr = [ "name" => $devicename ]; $arr_string = json_encode($arr);  curl_setopt($ch, curlopt_httpheader, array(     'content-type: application/json',     'content-length: ' . strlen($arr_string) ));  curl_setopt($ch, curlopt_postfields, $arr_string ); 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -