2017-03-02 20 views
0

我有一個使用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數組,另一個用於驗證+獲取所有文檔。

回答

1

錯誤的字面意思是,你不能重新聲明類「Cloudant」,因爲你的項目中有另一個地方 - 重命名這個類應該有所幫助。

對於來自PHP的Cloudant交談,這是一個HTTP接口,所以雖然有庫,但您並不一定需要一個用於您提到的一些簡單查詢。我通常使用Guzzle,但可以使用curl擴展或內置流處理程序。使用Guzzle與Cloudant交談的PHP代碼的一些示例位於我構建的示例應用程序中,這可能會幫助您提供一些指示:https://github.com/ibm-cds-labs/guestbook/blob/master/src/web/classes/Guestbook/CommentService.php

相關問題