Friday, November 15, 2013

Sending a PHP Curl requests to REST API of Parse.com

Parse is an external BaaS (Backend as a service) provider.

The REST API of Parse lets you interact with Parse from anything that can send an HTTP request. Read more about Parse Rest API.

Follow the steps to create a Parse App and obtain AppId.

1. SignUp for Parse
2. Move to Dashboard
3. Create a New App
4. Copy Application ID, Client Key and REST API Key to a text file.

Following is a sample code to send a Php Curl request to Rest API of Parse.  Replace the placeholders with your Application ID and Rest API Key in the code and run it from a php server.

 <?php  
 $url = 'https://api.parse.com/1/classes/GameScore';  
 $appId = 'YOUR_APP_ID';  
 $restKey = 'YOUR_REST_KEY';  
 $headers = array(  
   "Content-Type: application/json",  
   "X-Parse-Application-Id: " . $appId,  
   "X-Parse-REST-API-Key: " . $restKey  
 );  
 $objectData = '{"name":"Adarsh", "age":"26"}';  
 $rest = curl_init();  
 curl_setopt($rest,CURLOPT_URL,$url);  
 curl_setopt($rest,CURLOPT_POST,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;  
 print_r($response);  
 curl_close($rest);  
 ?>  


Goto the Data Browser tab of the parse to find the saved data :)

6 comments:

  1. this code return only one record which first match
    i want get all data

    ReplyDelete
  2. This code is used to insert data in Parse, Not to retrieve data.

    ReplyDelete
  3. Great work.

    I just have few question. How are that data being stored? Why the url is $url = 'https://api.parse.com/1/classes/GameScore';

    ReplyDelete
  4. Hi thanks for your post but i have a problem

    I have user table in Data browser and also i successfully fetched the user objectId but problem is when I update Using Rest -master -key I am not able to update fields
    can you help me

    ReplyDelete
  5. Thank you for sharing this information. This article is very interesting and useful. Keep up the good work!


    Thank you for sharing this information. This article is very interesting and useful. Keep up the good work!

    ReplyDelete