繁体   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