couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Couchdb Wiki] Update of "Getting_started_with_PHP" by NicoNam
Date Tue, 08 Dec 2009 15:49:18 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The "Getting_started_with_PHP" page has been changed by NicoNam.
http://wiki.apache.org/couchdb/Getting_started_with_PHP?action=diff&rev1=12&rev2=13

--------------------------------------------------

  Getting started with PHP and the CouchDB API.
  
  == CouchDB Libraries ==
- 
-  * PHPillow: [[http://kore-nordmann.de/projects/phpillow/]]
+  * PHPillow: http://kore-nordmann.de/projects/phpillow/
-  * PHP Object_Freezer: [[https://github.com/sebastianbergmann/php-object-freezer/tree]]
+  * PHP Object_Freezer: https://github.com/sebastianbergmann/php-object-freezer/tree
-  * PHP On Couch: [[http://github.com/dready92/PHP-on-Couch/tree/master]]
+  * PHP On Couch: http://github.com/dready92/PHP-on-Couch/tree/master
-  * PHP CouchDB Extension: [[http://www.topdog.za.net/php_couchdb_extension]]
+  * PHP CouchDB Extension: http://www.topdog.za.net/php_couchdb_extension
  
  == Setup ==
- 
  To get this example code running you need to install CouchDB on your system and have it
running on port 5984. If you use a different machine or port, change the first two lines of
code to your specific values.
  
  == What Does it Do? ==
- 
  This example first creates a !CouchSimple object that we're going to use for making connections
to CouchDB on our machine, port 5984. Notice that this response is already encoded in JSON.
CouchDB returns nothing but JSON wrapped in HTTP responses.
  
  It then tries to make a simple GET request to the root of the data store.
@@ -33, +30 @@

  
  At last, we delete our database. The HTTP DELETE method does the job. You can also DELETE
single documents in the same way.
  
+ {{{
- {{{<?php
+ <?php
+ 
-  $options['host'] = "localhost";
+  $options['host'] = "localhost"; 
   $options['port'] = 5984;
  
+  $couch = new CouchSimple($options); // See if we can make a connection
-  $couch = new CouchSimple($options);
- 
-  // See if we can make a connection
-  $resp = $couch->send("GET", "/");
+  $resp = $couch->send("GET", "/"); 
-  var_dump($resp);
-  // response: string(46) "{"couchdb": "Welcome", "version": "0.7.0a553"}"
+  var_dump($resp); // response: string(46) "{"couchdb": "Welcome", "version": "0.7.0a553"}"
  
- 
-  // Get a list of all databases in CouchDb
+  // Get a list of all databases in CouchDb 
-  $resp = $couch->send("GET", "/_all_dbs");
+  $resp = $couch->send("GET", "/_all_dbs"); 
-  var_dump($resp);
-  // response: string(17) "["test_suite_db"]"
+  var_dump($resp); // string(17) "["test_suite_db"]" 
  
   // Create a new database "test"
-  $resp = $couch->send("PUT", "/test");
+  $resp = $couch->send("PUT", "/test"); 
+  var_dump($resp); // string(12) "{"ok":true}" 
+  
-  var_dump($resp);
-  // response:
-  // string(12) "{"ok":true}
-  // "
- 
   // Get all documents in that database
-  $resp = $couch->send("GET", "/test/_all_docs");
+  $resp = $couch->send("GET", "/test/_all_docs"); 
-  var_dump($resp);
-  // response:
-  // string(27) "{"total_rows":0,"rows":[]}
+  var_dump($resp); // string(27) "{"total_rows":0,"rows":[]}" 
-  // "
  
   // Create a new document in the database test with the id 123 and some data
-  $resp = $couch->send("PUT", "/test/123", '{"_id":"123","data":"Foo"}');
+  $resp = $couch->send("PUT", "/test/123", '{"_id":"123","data":"Foo"}'); 
-  var_dump($resp);
-  // response:
-  // string(42) "{"ok":true,"id":"123","rev":"2039697587"}
+  var_dump($resp); // string(42) "{"ok":true,"id":"123","rev":"2039697587"}"   
-  // "
  
   // Get all documents in test again, seing doc 123 there
-  $resp = $couch->send("GET", "/test/_all_docs");
+  $resp = $couch->send("GET", "/test/_all_docs"); 
-  var_dump($resp);
-  // response:
-  // string(91) "{"total_rows":1,"offset":0,"rows":[{"id":"123","key":"123","value":{"rev":"2039697587"}}]}
+  var_dump($resp); // string(91) "{"total_rows":1,"offset":0,"rows":[{"id":"123","key":"123","value":{"rev":"2039697587"}}]}"

-  //"
  
   // Get back document with the id 123
-  $resp = $couch->send("GET", "/test/123");
+  $resp = $couch->send("GET", "/test/123"); 
-  var_dump($resp);
-  // response:
-  // string(47) "{"_id":"123","_rev":"2039697587","data":"Foo"}
+  var_dump($resp); // string(47) "{"_id":"123","_rev":"2039697587","data":"Foo"}" 
-  // "
  
   // Delete our "test" database
-  $resp = $couch->send("DELETE", "/test/");
+  $resp = $couch->send("DELETE", "/test/"); 
+  var_dump($resp); // string(12) "{"ok":true}"
-  var_dump($resp);
-  // response: string(12) "{"ok":true}
-  //"
  
-  class CouchSimple
+  class CouchSimple {
-  {
-      function CouchSimple($options)
+     function CouchSimple($options) {
-      {
-          foreach($options AS $key => $value) {
+        foreach($options AS $key => $value) {
-              $this->$key = $value;
+           $this->$key = $value;
-          }
+        }
-      }
+     } 
- 
+    
-      function send($method, $url, $post_data = NULL)
+    function send($method, $url, $post_data = NULL) {
-      {
-          $s = fsockopen($this->host, $this->port, $errno, $errstr);
+       $s = fsockopen($this->host, $this->port, $errno, $errstr); 
- 
-          if(!$s) {
+       if(!$s) {
-              echo "$errno: $errstr\n";
+          echo "$errno: $errstr\n"; 
-              return false;
+          return false;
-          }
+       } 
  
-          $request = "$method $url HTTP/1.0\r\nHost: localhost\r\n";
+       $request = "$method $url HTTP/1.0\r\nHost: localhost\r\n"; 
  
-          if($post_data) {
+       if($post_data) {
-              $request .= "Content-Length: ".strlen($post_data)."\r\n\r\n";
+          $request .= "Content-Length: ".strlen($post_data)."\r\n\r\n"; 
-              $request .= "$post_data\r\n";
+          $request .= "$post_data\r\n";
+       } 
-          } else {
+       else {
-              $request .= "\r\n";
+          $request .= "\r\n";
-          }
+       }
+ 
-          fwrite($s, $request);
+       fwrite($s, $request); 
- 
-          $response = "";
+       $response = ""; 
+ 
-          while(!feof($s)) {
+       while(!feof($s)) {
-              $response .= fgets($s);
+          $response .= fgets($s);
-          }
+       }
  
-          list($this->headers, $this->body) = explode("\r\n\r\n", $response);
+       list($this->headers, $this->body) = explode("\r\n\r\n", $response); 
-          return $this->body;
+       return $this->body;
-      }
+    }
+ }
-  }
- 
-  ?>
+ ?>
  }}}
  
  == A CouchDB Response Class ==
- 
  We'll use the following class as a structure for storing and handling responses to our HTTP
requests to the DB.  Instances of this will store response components, namely the headers
and body, in appropriately named properties.  Eventually we might want to do more error checking
based on the headers, etc.  For this example, we'll be most interested in ''CouchDBResponse::getBody()''.
 It returns either the text of the response or the data structure derived from decoding the
JSON response based on the method's only parameter, ''$decode_json''.  Inside the ''getBody''
method, we call a static method ''decode_json'' that lives in our as-yet-unwritten ''CouchDB''
class.  We'll get to that soon enough, but all it really does in this example is wrap a call
to the PHP json extension's ''json_decode'' function.
  
  {{{
- class CouchDBResponse {
+ class CouchDBResp onse {
  
      private $raw_response = '';
      private $headers = '';
@@ -147, +120 @@

          $this->raw_response = $response;
          list($this->headers, $this->body) = explode("\r\n\r\n", $response);
      }
-     
+ 
      function getRawResponse() {
          return $this->raw_response;
      }
-     
+ 
      function getHeaders() {
          return $this->headers;
      }
-     
+ 
      function getBody($decode_json = false) {
          return $decode_json ? CouchDB::decode_json($this->body) : $this->body;
      }
  }
  }}}
- 
  == A CouchDB Request Class ==
- 
  Now that we have a response class, we need something to organize our requests.  This class
will 1) build request headers and assemble the request, 2) send the request and 3) give us
the interesting part of the result.  Following [[GettingStartedWithPhp|Noah Slater's lead]],
we make our requests using ''fsockopen'', which allows us to treat our connection to the CouchDB
server as a file pointer.  When we execute the request, we pass the response on to a new ''CouchDBRequest''
object.
  
  {{{
  class CouchDBRequest {
  
      static $VALID_HTTP_METHODS = array('DELETE', 'GET', 'POST', 'PUT');
-     
+ 
      private $method = 'GET';
      private $url = '';
      private $data = NULL;
      private $sock = NULL;
      private $username;
      private $password;
-     
+ 
      function __construct($host, $port = 5984, $url, $method = 'GET', $data = NULL, $username
= null, $password = null) {
          $method = strtoupper($method);
          $this->host = $host;
@@ -187, +158 @@

          $this->data = $data;
          $this->username = $username;
          $this->password = $password;
-         
+ 
          if(!in_array($this->method, self::$VALID_HTTP_METHODS)) {
              throw new CouchDBException('Invalid HTTP method: '.$this->method);
          }
      }
-     
+ 
      function getRequest() {
          $req = "{$this->method} {$this->url} HTTP/1.0\r\nHost: {$this->host}\r\n";
-         
+ 
          if($this->username || $this->password)
              $req .= 'Authorization: Basic '.base64_encode($this->username.':'.$this->password)."\r\n";
  
@@ -206, +177 @@

          } else {
              $req .= "\r\n";
          }
-         
+ 
          return $req;
      }
-     
+ 
      private function connect() {
          $this->sock = @fsockopen($this->host, $this->port, $err_num, $err_string);
          if(!$this->sock) {
              throw new CouchDBException('Could not open connection to '.$this->host.':'.$this->port.'
('.$err_string.')');
-         }    
+         }
-     }
-     
+     }
+ 
      private function disconnect() {
          fclose($this->sock);
          $this->sock = NULL;
      }
-     
+ 
      private function execute() {
          fwrite($this->sock, $this->getRequest());
          $response = '';
@@ -231, +202 @@

          $this->response = new CouchDBResponse($response);
          return $this->response;
      }
-     
+ 
      function send() {
          $this->connect();
          $this->execute();
          $this->disconnect();
          return $this->response;
      }
-     
+ 
      function getResponse() {
          return $this->response;
      }
  }
  }}}
- 
  == The CouchDB Class ==
- 
  The CouchDB class provides a ''send'' method for sending requests to the CouchDB server.
 It uses the ''CouchDBRequest'' class above and returns a ''CouchDBResponse'' object.  This
class also provides a method for fetching all documents in a database, using the ''_all_docs''
built-in view.  I've also included a ''get_item'' method for fetching a document with its
id.  Clearly, further abstraction for different types of queries, etc. should follow, but
this is enough for us to get at the data in our database.
  
  Supports HTTP Basic Authentication for the whole session - just provide either the username,
password, or both when creating this class. The pair is then sent to the CouchDBRequest to
be included in the header.
@@ -264, +233 @@

          $this->username = $username;
          $this->password = $password;
      }
-     
+ 
      static function decode_json($str) {
          return json_decode($str);
      }
-     
+ 
      static function encode_json($str) {
          return json_encode($str);
      }
-     
+ 
      function send($url, $method = 'get', $data = NULL) {
          $url = '/'.$this->db.(substr($url, 0, 1) == '/' ? $url : '/'.$url);
          $request = new CouchDBRequest($this->host, $this->port, $url, $method, $data,
$this->username, $this->password);
          return $request->send();
      }
-     
+ 
      function get_all_docs() {
          return $this->send('/_all_docs');
      }
-     
+ 
      function get_item($id) {
          return $this->send('/'.$id);
      }
  }
  }}}
- 
  == Using Our CouchDB Class ==
- 
  The following is some code just playing around with our pastebin data, which we assume contains
the fields title, body, created, and status.
  
  {{{
@@ -325, +292 @@

  // note that we set the content type to 'text/javascript' for posts in our couchdb class.
  $view_result = $couchdb->send('/_temp_view', 'post', $view);
  print $view_result->getBody();
- 
  }}}
  

Mime
View raw message