Parse's REST API and PHP Curl requests - how t

2019-04-22 17:19发布

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条回答
不美不萌又怎样
2楼-- · 2019-04-22 17:34
 <?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);  
 ?>  
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-04-22 17:40

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);
查看更多
We Are One
4楼-- · 2019-04-22 17:52

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 ;

}
查看更多
登录 后发表回答