0

Using Parse.com and REST API. I am sending a PUT request in order to update a record.

$url = 'https://api.parse.com/1/classes/Language/lXn2Jr8g3D';  
$headers = array(  
  "Content-Type: application/json",  
  "X-Parse-Application-Id: " . $appId,  
  "X-Parse-REST-API-Key: " . $apiKey ,
);  
$objectData = '{"designation":"barfoo", "order":6}';  
$rest = curl_init();  
curl_setopt($rest,CURLOPT_URL,$url);  
curl_setopt($rest,CURLOPT_PUT,1);  
curl_setopt($rest,CURLOPT_POSTFIELDS,$objectData);  
curl_setopt($rest,CURLOPT_HTTPHEADER,$headers);  
curl_setopt($rest,CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($rest,CURLOPT_RETURNTRANSFER, true);  
$response = curl_exec($rest);  
echo $response;  
curl_close($rest); 

This reports back

{"updatedAt":"2015-02-09T22:28:57.676Z"}

I see the record and I don't see the changes I requested. BUT the updatedAt field is really update. In fact this is the only thing that gets updated! If I ommit the objectId from the url and use POST instead of PUT, the insertion works just fine.

denispyr
  • 1,403
  • 3
  • 21
  • 34

1 Answers1

0

The way to go was using INFILE instead of POSTFIELDS in order to pass the data (thx for the sample code).

$url = 'https://api.parse.com/1/classes/Language/lXn2Jr8g3D';  
$headers = array(  
  "Content-Type: application/json",  
  "X-Parse-Application-Id: " . $appId,  
  "X-Parse-REST-API-Key: " . $apiKey ,
);  
$objectData = '{"designation":"barfoo", "order":6}';
// ADDED THIS ---------------------------------------------
//trasnform string to file
$dataAsFile = tmpfile(); 
fwrite($dataAsFile, $objectData); 
fseek($dataAsFile, 0); 
// THIS ADDED ---------------------------------------------


$rest = curl_init();  
curl_setopt($rest,CURLOPT_URL,$url);  
curl_setopt($rest,CURLOPT_PUT,1);  
// REPLACED THIS ---------------------------------------------
// curl_setopt($rest,CURLOPT_POSTFIELDS,$objectData);  
curl_setopt($rest, CURLOPT_INFILE, $dataAsFile); 
curl_setopt($rest, CURLOPT_INFILESIZE, strlen($objectData));
// THIS REPLACED ---------------------------------------------
curl_setopt($rest,CURLOPT_HTTPHEADER,$headers);  
curl_setopt($rest,CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($rest,CURLOPT_RETURNTRANSFER, true);  
$response = curl_exec($rest);  
echo $response;  
curl_close($rest); 
Community
  • 1
  • 1
denispyr
  • 1,403
  • 3
  • 21
  • 34