【问题标题】:Load Json data from file into Parse.com backend via Rest API通过 Rest API 将 Json 数据从文件加载到 Parse.com 后端
【发布时间】:2014-01-26 13:02:12
【问题描述】:

晚安,

我正在尝试将 .json 文件中的数据加载到 parse.com 后端。根据 Parse.com 提供的示例,我可以让其余 api 从我的 Mac 上的终端加载手动添加的 json:

curl -X POST \
  -H "X-Parse-Application-Id: removed" \
  -H "X-Parse-REST-API-Key: removed" \
  -H "Content-Type: application/json" \
  -d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' \
  https://api.parse.com/1/classes/Test

这很好用。

如何读取仅包含这三个参数的已保存 .Json 文件(Json 验证良好)并将数据发送到我的 Parse 后端? "score":1337,"playerName":"Sean Plott","cheatMode":false

我相信我可以选择使用 PHP/Jquery 等。但是我没有广泛使用这些语言中的任何一种,非常感谢快速代码示例。

谢谢,

杰拉德

【问题讨论】:

  • 你可以试试curl -X POST -H "X-Parse-Application-Id: removed" -H "X-Parse-REST-API-Key: removed" -H "Content-Type: application/json" -d @file.json https://api.parse.com/1/classes/Test
  • 嗨,Prashant,感谢您的回答,这个 curl 只是在 Parse 类中添加了一行,但它是空的,我将快速浏览一下您的 PHP 解决方案。如果文件设置为 -d @file.json 它应该从上面的代码中解析并添加每个结果吗?

标签: php jquery json rest parse-platform


【解决方案1】:

这里是纯PHP解决方案:

$url = 'https://api.parse.com/1/classes/Test';
$fields_string = file_get_contents('file.json');

//open connection
$ch = curl_init();

//set the url, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);
/* End of first request */

【讨论】:

    猜你喜欢
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 2021-11-10
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多