使用 PHP 在 Cloudant 中进行简单操作
Making simple operations in Cloudant using PHP
我有一个使用 AngularJS 构建的简单 SPA。
我现在正在尝试使用 PHP 文件在 Cloudant 中托管的 CouchDB 中执行 2 个简单操作:保存文档并获取所有文档。
所有使用 PHP 的 CRUD 操作的预制示例都非常复杂,使用多个依赖项,我没有深入了解它们,因为 PHP 不是我的菜。
我尝试的最后一个是 this one,它给了我一个 Cannot redeclare class Cloudant in /.../comanda/incoming/test/Cloudant.php on line 10
错误,我没能修复它。
不过,我在代码中看到的是它们如何让服务器登录,以及一些 get/put.
看起来像这样:
class Cloudant {
function __construct($server,$db,$username,$password){
$username = 'user';
$password = 'pass';
$server = 'cloudant.com';
$db = 'dbname';
$this->server = "https://".$username.":".$password.'@'.$username.$server;
$this->db = $db;
$this->header_short = "Content-Type: application/json";
$this->header = $this->header_short."\r\nContent-Length: ";
}
//curl -X PUT {USERNAME}:{PASSWORD}@{USERNAME}.cloudant.com/{DB}{/ID} -d ....
function upsert_doc( $id, $data ){ // to update - include _rev in document body
return $this->call($id, array(
'method' => 'POST',
'header' => $this->header . strlen($data) . "\r\n",
'content' => $data));
}
//curl -X GET {USERNAME}:{PASSWORD}@{USERNAME}.cloudant.com/{DB}/{ID}
function get( $id ){
return $this->call($id, array(
'method' => 'GET',
'header' => $this->header_short
));
}
关于如何简化这个的任何提示?我正在寻找一个 PHP 文件来制作身份验证 + 保存 JSON 数组,另一个文件来进行身份验证 + 获取所有文档。
错误实际上是您无法重新声明 class "Cloudant",因为您的项目中某处还有另一个 - 重命名此 class 应该会有所帮助。
为了从 PHP 与 Cloudant 对话,它是一个 HTTP 接口,因此虽然有库,但您不一定需要一个库来进行您提到的一些简单查询。我通常使用 Guzzle,但您可以使用 curl 扩展或内置流处理程序。使用 Guzzle 与 Cloudant 对话的一些 PHP 代码示例位于我构建的示例应用程序中,这可能有助于为您提供一些指导:https://github.com/ibm-cds-labs/guestbook/blob/master/src/web/classes/Guestbook/CommentService.php
我有一个使用 AngularJS 构建的简单 SPA。 我现在正在尝试使用 PHP 文件在 Cloudant 中托管的 CouchDB 中执行 2 个简单操作:保存文档并获取所有文档。
所有使用 PHP 的 CRUD 操作的预制示例都非常复杂,使用多个依赖项,我没有深入了解它们,因为 PHP 不是我的菜。
我尝试的最后一个是 this one,它给了我一个 Cannot redeclare class Cloudant in /.../comanda/incoming/test/Cloudant.php on line 10
错误,我没能修复它。
不过,我在代码中看到的是它们如何让服务器登录,以及一些 get/put.
看起来像这样:
class Cloudant {
function __construct($server,$db,$username,$password){
$username = 'user';
$password = 'pass';
$server = 'cloudant.com';
$db = 'dbname';
$this->server = "https://".$username.":".$password.'@'.$username.$server;
$this->db = $db;
$this->header_short = "Content-Type: application/json";
$this->header = $this->header_short."\r\nContent-Length: ";
}
//curl -X PUT {USERNAME}:{PASSWORD}@{USERNAME}.cloudant.com/{DB}{/ID} -d ....
function upsert_doc( $id, $data ){ // to update - include _rev in document body
return $this->call($id, array(
'method' => 'POST',
'header' => $this->header . strlen($data) . "\r\n",
'content' => $data));
}
//curl -X GET {USERNAME}:{PASSWORD}@{USERNAME}.cloudant.com/{DB}/{ID}
function get( $id ){
return $this->call($id, array(
'method' => 'GET',
'header' => $this->header_short
));
}
关于如何简化这个的任何提示?我正在寻找一个 PHP 文件来制作身份验证 + 保存 JSON 数组,另一个文件来进行身份验证 + 获取所有文档。
错误实际上是您无法重新声明 class "Cloudant",因为您的项目中某处还有另一个 - 重命名此 class 应该会有所帮助。
为了从 PHP 与 Cloudant 对话,它是一个 HTTP 接口,因此虽然有库,但您不一定需要一个库来进行您提到的一些简单查询。我通常使用 Guzzle,但您可以使用 curl 扩展或内置流处理程序。使用 Guzzle 与 Cloudant 对话的一些 PHP 代码示例位于我构建的示例应用程序中,这可能有助于为您提供一些指导:https://github.com/ibm-cds-labs/guestbook/blob/master/src/web/classes/Guestbook/CommentService.php