Php 是否可以通过Mongolab';什么是RESTful API?

Php 是否可以通过Mongolab';什么是RESTful API?,php,mongodb,rest,Php,Mongodb,Rest,我想在后端将准备好的JSON发送到我的PHP脚本,然后使用Mongolab的RESTful API进行插入。我能找到的关于插入的唯一例子是前端AJAX 来自的示例代码 翻译成PHP的jQuery示例如下: <?php $key = "my API key"; $db = "my-database-name"; $collection = "my-collection-name"; $document = array( "x" => 1,

我想在后端将准备好的JSON发送到我的PHP脚本,然后使用Mongolab的RESTful API进行插入。我能找到的关于插入的唯一例子是前端AJAX

来自的示例代码


翻译成PHP的jQuery示例如下:

<?php
$key        = "my API key";
$db         = "my-database-name";
$collection = "my-collection-name";

$document = array(
    "x" => 1,
    "more" => array("data", "here"),
);

$opts = array(
    "http" => array(
        "method"  => "POST",
        "header"  => "Content-type: application/json",
        "content" => json_encode($document),
    ),
);

$context = stream_context_create($opts);

$result = file_get_contents("https://api.mongolab.com/api/1/databases/$db/collections/$collection?apiKey=$key", false, $context);

var_dump($result); // Dumps the response document
?>


这里真正的问题是,既然您使用的是PHP,它确实有一个本机驱动程序实现,那么为什么首先要使用RESTAPI?使用本机驱动程序有什么问题?很遗憾,我无法在此特定服务器上安装MongoDB驱动程序。这就是我被迫使用RESTful API的原因。
$data = file_get_contents('https://api.mongolab.com/api/1/databases/**DATABASE**/collections/**COLLECTION*?q={%22_id%22:{%22$oid%22:%22' . $_GET['id'] . '%22}}&apiKey=**API KEY**');

$obj = json_decode($data);
$obj=$obj[0];
<?php
$key        = "my API key";
$db         = "my-database-name";
$collection = "my-collection-name";

$document = array(
    "x" => 1,
    "more" => array("data", "here"),
);

$opts = array(
    "http" => array(
        "method"  => "POST",
        "header"  => "Content-type: application/json",
        "content" => json_encode($document),
    ),
);

$context = stream_context_create($opts);

$result = file_get_contents("https://api.mongolab.com/api/1/databases/$db/collections/$collection?apiKey=$key", false, $context);

var_dump($result); // Dumps the response document
?>