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 SamBisbee
Date Thu, 06 Aug 2009 21:26:26 GMT
Dear Wiki user,

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

The following page has been changed by SamBisbee:
http://wiki.apache.org/couchdb/Getting_started_with_PHP

The comment on the change is:
Adds HTTP Basic Auth to complex classes - assumes combo won't change for session

------------------------------------------------------------------------------
      private $url = '';
      private $data = NULL;
      private $sock = NULL;
+     private $username;
+     private $password;
      
-     function __construct($host, $port = 5984, $url, $method = 'GET', $data = NULL) {
+     function __construct($host, $port = 5984, $url, $method = 'GET', $data = NULL, $username
= null, $password = null) {
          $method = strtoupper($method);
          $this->host = $host;
          $this->port = $port;
          $this->url = $url;
          $this->method = $method;
          $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);
@@ -189, +193 @@

      
      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";
+ 
          if($this->data) {
              $req .= 'Content-Length: '.strlen($this->data)."\r\n";
              $req .= 'Content-Type: application/json'."\r\n\r\n";
@@ -239, +247 @@

  
  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.
+ 
  {{{
  class CouchDB {
  
+     $this->username;
+     $this->password;
+ 
-     function __construct($db, $host = 'localhost', $port = 5984) {
+     function __construct($db, $host = 'localhost', $port = 5984, $username = null, $password
= null) {
          $this->db = $db;
          $this->host = $host;
          $this->port = $port;
+         $this->username = $username;
+         $this->password = $password;
      }
      
      static function decode_json($str) {
@@ -258, +273 @@

      
      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);
+         $request = new CouchDBRequest($this->host, $this->port, $url, $method, $data,
$username, $password);
          return $request->send();
      }
      

Mime
View raw message