我使用出色的Parse作为数据存储,但需要通过PHP访问它(作为一个可能无关紧要的细节-我必须通过PHP访问,以便Facebook scraper识别我页面上动态生成的标签)。
Parse有一个Rest API,以及关于如何使用它们的基本说明。例如,检索对象:
curl-X获取''
-H"X-Parse-Application-Id:[我的应用程序Id]"''
-H"X-Parse-REST-API-Key:[My Parse REST API Key]"''
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 ;
}