2017-03-02 7 views
0

저는 AngularJS로 만든 간단한 SPA를 가지고 있습니다. PHP 파일을 사용하여 Cloudant에서 호스팅되는 CouchDB에서 두 가지 간단한 작업을 수행하려고합니다. 문서를 저장하고 모든 문서를 가져옵니다.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을 사용하지만 컬 확장이나 내장 된 스트림 핸들러를 사용할 수 있습니다. Guzzle을 사용하여 Cloudant와 대화하는 PHP 코드의 예가 제가 작성한 샘플 앱에 있습니다. 이는 다음과 같이 포인터를 제공합니다. https://github.com/ibm-cds-labs/guestbook/blob/master/src/web/classes/Guestbook/CommentService.php