1

I am having trouble converting my curl command into php.

This part works great.

CURL command that adds an entry into my Parse.com database:

curl -X POST \
  -H "X-Parse-Application-Id: my_id" \
  -H "X-Parse-REST-API-Key: api_id" \
  -H "Content-Type: application/json" \
  -d "{\"SiteID\":\"foundID\",\"dataUsedString\":\"foundUsage\",\"usageDate\":\"foundDate\", \"monthString\":\"foundMonth\", \"dayString\":\"foundDay\",\"yearString\":\"foundYear\"}" \
  https://api.parse.com/1/classes/MyClass

SOLVED ANSWER:

I have created this php script to replicate the command:

   <?php 
   $ch = curl_init('https://api.parse.com/1/classes/MyClass');

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

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"SiteID\":\"foundID\",\"dataUsedString\":\"foundUsage\",\"usageDate\":\"foundDate\", \"monthString\":\"foundMonth\", \"dayString\":\"foundDay\",\"yearString\":\"foundYear\"}");

curl_exec($ch);
curl_close($ch);
?>
2
  • What response do you get from the remote server? Commented Jul 15, 2014 at 17:05
  • SOLVED!!!! Thank you @valentin and cOle2 Commented Jul 15, 2014 at 17:16

2 Answers 2

1

You've missed some crucial configurations. These are the set the CURL to send request using POST, and the second is data to send. (RAW DATA is being sent as string into POSTFIELDS, those if you send array - it will automatically append header "multipart/form-data"

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

curl_setopt($ch,CURLOPT_HTTPHEADER,
  array(
    'X-Parse-Application-Id:my_id',
    'X-Parse-REST-API-Key:api_id',
    'Content-Type: application/json'
  )
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"SiteID\":\"foundID\",\"dataUsedString\":\"foundUsage\",\"usageDate\":\"foundDate\", \"monthString\":\"foundMonth\", \"dayString\":\"foundDay\",\"yearString\":\"foundYear\"}");
curl_exec($ch);
curl_close($ch);

HTH:)

Sign up to request clarification or add additional context in comments.

1 Comment

I put my updated my code to what you suggested. I edited my code above so that you can see it. A row is still not being added into my database. Any more suggestions?
0

Since you are doing a POST request you need to tell Curl to do that as well:

$postData = '{"SiteID":"foundID","dataUsedString":"foundUsage","usageDate":"foundDate", "monthString":"foundMonth", "dayString":"foundDay","yearString":"foundYear"}';

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

You may also need to supply a Content-Length header:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'X-Parse-Application-Id: my_id',
  'X-Parse-REST-API-Key: api_id',
  'Content-Type: application/json',                                                           
  'Content-Length: '.strlen($postData))                                                                       
);

1 Comment

I edited my code and my answer to add my updated code. But still, a new row is not being created. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.