WWW.DUMAIS.IO
ARTICLES
OVERLAY NETWORKS WITH MY SDN CONTROLLERSIMPLE LEARNING SWITCH WITH OPENFLOWINSTALLING KUBERNETES MANUALLYWRITING A HYPERVISOR WITH INTEL VT-X CREATING YOUR OWN LINUX CONTAINERSVIRTIO DRIVER IMPLEMENTATIONNETWORKING IN MY OSESP8266 BASED IRRIGATION CONTROLLERLED STRIP CONTROLLER USING ESP8266.OPENVSWITCH ON SLACKWARESHA256 ASSEMBLY IMPLEMENTATIONPROCESS CONTEXT ID AND THE TLBTHREAD MANAGEMENT IN MY HOBBY OSENABLING MULTI-PROCESSORS IN MY HOBBY OSNEW HOME AUTOMATION SYSTEMINSTALLING AND USING DOCKER ON SLACKWARESYSTEM ON A CHIP EMULATORUSING JSSIP AND ASTERISK TO MAKE A WEBPHONEC++ WEBSOCKET SERVERSIP ATTACK BANNINGBLOCK CACHING AND WRITEBACKBEAGLEBONE BLACK BARE METAL DEVELOPEMENTARM BARE METAL DEVELOPMENTUSING EPOLLMEMORY PAGINGIMPLEMENTING HTTP DIGEST AUTHENTICATIONSTACK FRAME AND THE RED ZONE (X86_64)AVX/SSE AND CONTEXT SWITCHINGHOW TO ANSWER A QUESTION THE SMART WAY.REALTEK 8139 NETWORK CARD DRIVERREST INTERFACE ENGINECISCO 1760 AS AN FXS GATEWAYHOME AUTOMATION SYSTEMEZFLORA IRRIGATION SYSTEMSUMP PUMP MONITORINGBUILDING A HOSTED MAILSERVER SERVICEI AM NOW HOSTING MY OWN DNS AND MAIL SERVERS ON AMAZON EC2DEPLOYING A LAYER3 SWITCH ON MY NETWORKACD SERVER WITH RESIPROCATEC++ JSON LIBRARYIMPLEMENTING YOUR OWN MUTEX WITH CMPXCHGWAKEUPCALL SERVER USING RESIPROCATEFFT ON AMD64CLONING A HARD DRIVECONFIGURING AND USING KVM-QEMUUSING COUCHDBINSTALLING COUCHDB ON SLACKWARENGW100 MY OS AND EDXS/LSENGW100 - MY OSASTERISK FILTER APPLICATIONCISCO ROUTER CONFIGURATIONAASTRA 411 XML APPLICATIONSPA941 PHONEBOOKSPEEDTOUCH 780 DOCUMENTATIONAASTRA CONTACT LIST XML APPLICATIONAVR32 OS FOR NGW100ASTERISK SOUND INJECTION APPLICATIONNGW100 - DIFFERENT PROBLEMS AND SOLUTIONSAASTRA PRIME RATE XML APPLICATIONSPEEDTOUCH 780 CONFIGURATIONUSING COUCHDB WITH PHPAVR32 ASSEMBLY TIPAP7000 AND NGW100 ARCHITECTUREAASTRA WEATHER XML APPLICATIONNGW100 - GETTING STARTEDAASTRA ALI XML APPLICATION

USING COUCHDB WITH PHP

2012-02-25

Introduction

Since couchdb provides a REST interface, it is tempting use AJAX to query the database directly to display data on your webpage. This is not always possible though. If my database runs on port 5555 then this means that my webpage will need access to port 5555 on my server. It's not always possible if firewalls are in the way. We know at least that port 80 should always be accessible. For several security reasons too, we might want the CouchDB to be hidden behind the public network and only accessible by the web server internally. Just as we would do with a SQL server.

Code

  • Retrieve a document $h = curl_init("http://server/db1/document1"); curl_setopt($h, CURLOPT_RETURNTRANSFER, true); curl_setopt($h, CURLOPT_CUSTOMREQUEST, "GET"); $response = curl_exec($h); $obj = json_decode($response);
  • Create views
    Json file: { "language": "javascript", "views": { "view1": { "map": "function(doc){emit(doc._id,doc);}" }, "view2": { "map": "function(doc){emit(doc._id,doc);}", "reduce": "function (key, values){return null;}" } } } PHP script: $json = file_get_contents("views.json"); $h = curl_init("http://server/db1/_design/designdoc1"); curl_setopt($h, CURLOPT_RETURNTRANSFER, true); curl_setopt($h, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($h, CURLOPT_POSTFIELDS,$json); curl_setopt($h, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); $response = curl_exec($h);
  • Create doc $obj = array("field1"=>"val1","field2"=>"val2"); $json = json_encode($obj); $h = curl_init("http://server/db1/document1"); curl_setopt($h, CURLOPT_RETURNTRANSFER, true); curl_setopt($h, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($h, CURLOPT_POSTFIELDS,$json); curl_setopt($h, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); $response = curl_exec($h);
  • Query a view $h = curl_init("http://server/db1/_design/document1/_view/view1?reduce=true&group=true"); curl_setopt($h, CURLOPT_RETURNTRANSFER, true); curl_setopt($h, CURLOPT_CUSTOMREQUEST, "GET"); $response = curl_exec($h); $json=json_decode($response);

Retrieving attachments

Let's say we have an attachment that can be reachable at http://couchdbServer/database1/document1/image1.jpg. If "document1" is a blog post that show "image1", it is tempting to put a <img src=database1/document1/image1.jpg > But this is impossible, because that server is inaccessible from the outside world. So we might wanna do a proxy script:

$att = $_GET["a"]; $article = $_GET["art"]; $h = curl_init("http://".COUCHDB_SERVER."/".COUCHDB_DATABASE."/$article/$att"); curl_setopt($h, CURLOPT_RETURNTRANSFER, true); curl_setopt($h, CURLOPT_CUSTOMREQUEST, "GET"); $response = curl_exec($h); $contentType = curl_getinfo($h, CURLINFO_CONTENT_TYPE); header("Content-type: $contentType"); echo $response;

That way we can use our image like this: <img src=attachment.php?doc=document1&attachment=image1.jpg >