Parse.com Create New Object Using PHP CURL
Parse.com is an external BaaS (Backend as a service) provider.
Parse.com provides REST API that lets you interact with Parse.com from anything that can send an HTTP request. In this tutorial we will send and HTTP request to Parse.com using PHP CURL.
This tutorial assumes that you have Parse.com account. Follow the below mentioned steps to create a Parse.com App and obtain Application Id.
- Sign In to Parse.com
- Move to Dashobard
- Create a New App
- Copy Application ID, Client Key and REST API Key
The code below will send a PHP CURL request to REST API of Parse. To create a new object on Parse, send a POST request to the class URL containing the contents of the object. You have to place you Application ID and REST API Key with the placeholders. You can run this code on a PHP server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php $url = 'https://api.parse.com/1/classes/TestObject'; $headers = array( "X-Parse-Application-Id: " . YOUR_APPLICATION_ID, "X-Parse-REST-API-Key: " . YOUR_REST_API_KEY, "Content-Type: application/json" ); $object_data = '{'name':"David","age":"28","sex":"male","dob":"15-May-1986"}'; $rest = curl_init(); curl_setopt($rest, CURLOPT_URL, $url); curl_setopt($rest, CURLOPT_POST, 1); curl_setopt($rest, CURLOPT_POSTFIELDS, $object_data); curl_setopt($rest, CURLOPT_HTTPHEADER, $headers); curl_setopt($rest, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($rest, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($rest); print_r(json_decode($response)); curl_close($rest); ?> |
When the creation is successful, the HTTP response is a 201 Created and the Location header contains the object URL for the new object:
1 2 |
Status: 201 Created Location: https://api.parse.com/1/classes/GameScore/Ed1nuqPvcm |
The response body is a JSON object containing the objectId and the createdAt timestamp of the newly-created object:
1 2 3 4 |
{ "createdAt": "2011-08-20T02:06:57.931Z", "objectId": "Ed1nuqPvcm" } |