我有一個使用AngularJS構建的簡單SPA。 我現在嘗試在使用PHP文件託管的CouchDB中執行2個簡單操作:保存文檔並獲取所有文檔。使用PHP在Cloudant中進行簡單操作
所有使用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數組,另一個用於驗證+獲取所有文檔。