Php CouchDB
For PHP CouchDB connectivity a PHP script is needed to be executed. By default, CouchDB executes on the port 5984. Follow the below steps for PHP CouchDB connectivity.
- Create a Php file with the below code.
index.php:<?php $options['host'] = "localhost"; $options['port'] = 5984; $couch = new CouchSimple($options); $couch->send("GET", "/"); $couch->send("PUT", "/example"); $couch->send("PUT", "/example/100", '{"_id":"100","name":"Tom"}'); $resp = $couch->send("GET", "/example/100"); echo $resp; class CouchSimple { function CouchSimple($options) { foreach($options AS $key => $value) { $this->$key = $value; } } function send($method, $url, $post_data = NULL) { $s = fsockopen($this->host, $this->port, $errno, $errstr); if(!$s) { echo "$errno: $errstr\n"; return false; } $request = "$method $url HTTP/1.0\r\nHost: $this->host\r\n"; if ($this->user) { $request .= "Authorization: Basic ".base64_encode("$this->user:$this->pass")."\r\n"; } if($post_data) { $request .= "Content-Length: ".strlen($post_data)."\r\n\r\n"; $request .= "$post_data\r\n"; } else { $request .= "\r\n"; } fwrite($s, $request); $response = ""; while(!feof($s)) { $response .= fgets($s); } list($this->headers, $this->body) = explode("\r\n\r\n", $response); return $this->body; } } ?>
- Access CouchDB using the URL: http://localhost:5984/_utils.
- Check available databases.
- Execute the PHP script at the localhost server.
- Access CouchDB again.
- A new database named “example” is created with a document referred to as ID 100.
- Values stored in the document can be checked by clicking on that.
- Fetch Data in the browser.