簡體   English   中英

Parse的REST API和PHP Curl請求 - 如何

[英]Parse's REST API and PHP Curl requests - how to

我使用優秀的Parse作為數據存儲,但需要通過PHP訪問它(作為一個可能無關緊要的細節 - 我必須通過PHP訪問它,以便Facebook刮刀識別我的頁面上的動態生成的標簽)。

Parse有一個Rest API,以及如何使用它們的基本指令。 例如,要檢索對象:

卷曲-X GET \\
-H“X-Parse-Application-Id:[我的應用程序ID]”
-H“X-Parse-REST-API-Key:[My Parse Rest API密鑰]”
https://api.parse.com/1/classes/moods/

不幸的是,我不知道如何將其與我在網上看到的PHP Curl示例集成。 我收集:

curl_setopt($ ch,CURLOPT_USERPWD,

..可能參與其中。 可能:

curl_setopt($ ch,CURLOPT_URL,$ Url);

但我可能會離開。 我很遺憾無法自己解決這個問題 - 但我認為這仍然是一個有效的問題,因為它對那些之前沒有使用過Curl / PHP的人來說非常困惑。 基本上 - 我正在尋找基本信息,如何從Parse文檔中引用示例...

提前致謝

編輯:

嘿所有,這是我配置它的解決方案。 感謝debianek讓我朝着正確的方向前進。

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;            

..等等。 希望有所幫助。

把它放入標題中

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

$handle = curl_init(); 
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers);
 <?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);  
 ?>  

將此函數用於所有對象插入,該對象應創建為具有正確索引的關聯數組

 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 ;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM