9

I am using the excellent Parse as a data store, but need to access it through PHP (as a perhaps irrelevant bit of detail - I am having to access it through PHP in order for Facebook scrapers to recognise dynaically generated tags on my page).

Parse have a Rest API, and basic instructions on how to use them. For example, to retrieve an object:

curl -X GET \
-H "X-Parse-Application-Id: [My application ID]" \
-H "X-Parse-REST-API-Key: [My Parse Rest API key]" \
https://api.parse.com/1/classes/moods/

Unfortunately, I have no idea how to integrate this with PHP Curl examples I've seen online. I gather:

curl_setopt($ch, CURLOPT_USERPWD,

..might be involved. As might:

curl_setopt($ch, CURLOPT_URL, $Url);

but I could be way off. I am really sorry to not be able to figure this out myself - but I think this is still a valid question as it is very confusing to those who haven't used Curl/PHP before. Basically - I'm looking for information as basic as where to put the quoted example from the Parse docs...

Thanks in advance

EDIT:

Hey all, here is the solution as I configured it. Thanks to debianek for getting me going in the right direction.

if ($_GET['id']) {
$imageId = $_GET['id']; 
MyApplicationId = '[ID]';
$MyParseRestAPIKey = '[API Key]';
$url = 'https://api.parse.com/1/classes/images/'.$imageId;

$headers = array(
    "Content-Type: application/json",
    "X-Parse-Application-Id: " . $MyApplicationId,
    "X-Parse-REST-API-Key: " . $MyParseRestAPIKey
);

    $handle = curl_init(); 
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);

    $data = curl_exec($handle);
curl_close($handle);

$array = json_decode($data);

$title =  $array->title;            

..and so on. Hope that helps.

3
  • Hello, you said you figured how to use it. Could you post here the solution Commented Aug 6, 2012 at 18:57
  • In the edit. Apologies for the delay. Commented Aug 7, 2012 at 12:26
  • The solution should be in a separate answer, not in the question itself. Remove it from here and put it into a new answer. Commented Oct 13, 2016 at 18:21

3 Answers 3

6

Put it into headers

$headers = array(
    "X-Parse-Application-Id: $MyApplicationId",
    "X-Parse-REST-API-Key: $MyParseRestAPIkey"
);

$handle = curl_init(); 
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks very much. With a bit of extra tweaking I got this working. I will edit my original post to reflect this when I have a complete solution. Which leads to my next sub-question. I have, I believe, a JSON object in my PHP. The HTTP_HEADER text output confirms it was fetched correctly. $data = curl_exec($handle); $array = json_decode($data, true); When I try and echo out $data in my html body, however, I get "error: unauthorised" and echo-ing out $array echoes out the word "array." Trying $array['objectId'] or $array[0].objectId returns a blank. Any further help massively appreciated..
4
 <?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);  
 ?>  

Comments

1

use this function for all object insert ,the object that should be created as associative array with correct index

 function insertObject($classname,$object){
 $ appId="xxxxx";
   $restKey="xxxx" 
    $url = 'https://api.parse.com/1/classes/'.$classname;
    $rest = curl_init();
        curl_setopt($rest, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($rest, CURLOPT_FRESH_CONNECT, FALSE);//use cache if available
        curl_setopt($rest, CURLOPT_TIMEOUT, 10); //10 sec timeout
        curl_setopt($rest,CURLOPT_URL,$url);
        curl_setopt($rest,CURLOPT_PORT,443);
        curl_setopt($rest,CURLOPT_POST,1);
        curl_setopt($rest,CURLOPT_POSTFIELDS,  json_encode($object));
        curl_setopt($rest,CURLOPT_HTTPHEADER,
            array("X-Parse-Application-Id: " . $appId,
                "X-Parse-REST-API-Key: " . $restKey,
                "Content-Type: application/json"));

        curl_setopt($rest, CURLOPT_SSL_VERIFYPEER, false);//to avoid certificate error
        $response = curl_exec($rest);
        echo $response ;

}

Comments

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.