Search code examples
phpcurlparse-platformput

Trouble converting PUT from command line to php


I am having trouble converting a PUT curl command into php. I just figured out how to convert a POST method into PHP but I am having a difficult time with using PUT to update my database on Parse.com.

This is my curl statement:

  curl -X PUT \
  -H "X-Parse-Application-Id: app id" \  
  -H "X-Parse-REST-API-Key: rest api"   
  -H "Content-Type: application/json"   
  -d '{"site_status":"statusValue"}'   
  https://api.parse.com/1/classes/MyClass/objectId 

(SOLVED): The information is being updated to Parse!!!

<?php 

 $ch = curl_init('https://api.parse.com/1/classes/ClientCompanyInfo/objectID');

  curl_setopt($ch,CURLOPT_HTTPHEADER,
      array('X-Parse-Application-Id:app_id',
      'X-Parse-REST-API-Key:rest_api_id',
      'Content-Type: application/json'));

 curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"PUT");
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"site_status\":\"siteValueStatus\"}");

 curl_exec($ch);
 curl_close($ch);

?>

Solution

  • Give this a shot:

    $headers = array(
      'X-Parse-Application-Id: app_id',
      'X-Parse-REST-API-Key: rest_key',
      'Content-Type: application/json'
    );
    $url = 'https://api.parse.com/1/classes/MyClass/objectId';
    $changes = array(
      'site_status' => 'statusValue'
    );
    $rest = curl_init();
    curl_setopt($rest, CURLOPT_URL, $url);
    curl_setopt($rest, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($rest, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($rest, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($rest, CURLOPT_POSTFIELDS, http_build_query($changes));
    $response = curl_exec($rest);
    curl_close($rest);
    var_dump($response);