2012-07-31 22 views
9

我正在使用優秀的Parse作爲數據存儲,但需要通過PHP訪問它(作爲一個可能無關緊要的細節 - 我必須通過PHP按順序訪問它Facebook刮板識別我的頁面上的動態生成的標籤)。解析的REST API和PHP Curl請求 - 如何

解析有一個Rest API,以及如何使用它們的基本說明。例如,檢索對象:

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/

不幸的是,我不知道如何將它與PHP捲曲的例子,我在網上看到整合。我收集:

curl_setopt($ch, CURLOPT_USERPWD,

..可能參與。正如人們:

curl_setopt($ch, CURLOPT_URL, $Url);

但我可能是遙遠。我非常抱歉無法自己弄清楚這一點 - 但我認爲這仍然是一個有效的問題,因爲以前沒有使用過Curl/PHP的人對此非常困惑。基本上 - 我提前找一樣基本從何處解析文檔把引用的例子信息...

感謝

編輯:

嘿,這裏是解決方案,我配置它。感謝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;    

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

+0

您好創建的對象,你說你想通如何使用它。你可以在這裏發佈解決方案 – 2012-08-06 18:57:44

+0

在編輯中。對延遲抱歉。 – 2012-08-07 12:26:19

+0

解決方案應該在一個單獨的答案中,而不是在問題本身。從這裏刪除並將其放入一個新的答案。 – Jamal 2016-10-13 18:21:19

回答

6

把它放進頭

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

$handle = curl_init(); 
curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); 
+0

非常感謝。隨着一些額外的調整,我得到了這個工作。當我有一個完整的解決方案時,我將編輯我的原始文章以反映這一點。這導致我的下一個子問題。 我相信我的PHP中有一個JSON對象。 HTTP_HEADER文本輸出確認它被正確提取。 $數據= curl_exec($處理); $ array = json_decode($ data,true); 當我嘗試在我的HTML體回聲出$的數據,但是,我得到「錯誤:未授權」和回聲-ING出$陣列呼應出單詞「陣列」。嘗試$ array ['objectId']或$ array [0] .objectId返回一個空格。任何進一步的幫助,大量讚賞.. – 2012-07-31 13:30:55

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); 
?> 
1

使用該功能適用​​於所有對象的插入,應作爲關聯數組與正確的索引

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 ; 

}