Use PHP and Curl to Work With CouchDB

Leave your reply

Introduction

Learn how to use CouchDB with PHP. Although MySQL is often considered the default choice when choosing a database for a PHP project, CouchDB is an excellent alternative option for many projects.

Why Choose CouchDB Over MySQL?

CouchDB accepts JSON queries and documents through its API. JSON is increasingly ubiquitous, and for developers who are familiar with JSON but not interested in learning SQL, CouchDB is a natural choice.

A CouchDB document is the equivalent of a MySQL record in a table. CouchDB is designed so that each document can have its own structure. Unlike MySQL, CouchDB is designed to let you add fields or leave them blank without having to change the entire database structure.

CouchDB also offers a simple and easy replication method which will automatically replicate the database, either locally or on a remote host, when any changes are made.

CouchDB is not a relational database like MySQL. However, many projects do not call for a relational database. If you are simply inserting data into a table and/or pulling it out of a table to display it on a page, a relational database is unnecessary.

Requirements

  • A Servidor Cloud (Ubuntu 16.04) with CouchDB installed.

To install CouchDB, follow the instructions in our article Install and Use CouchDB on Ubuntu 16.04. For information on basic CouchDB commands, see our article Working With CouchDB From the Command Line.

Example CouchDB Database

For this tutorial we will be using a simple database containing reviews of a local restaurant. Use the following commands from the command line to create this database and insert a sample record:

curl -X PUT http://127.0.0.1:5984/reviews
curl -X PUT http://127.0.0.1:5984/reviews/01 -d '{"reviewer_name":"Ben", "stars":"5", "details":"Love the calzone!"}'

Using Curl to Work With CouchDB

First, you will need to install the php-curl package on your server:

  • PHP 7.0+sudo apt-get install php-curl
  • PHP5sudo apt-get install php5-curl

Then restart Apache for the changes to take effect:

sudo systemctl restart apache2

Note: Use the following command to see the installed version of PHP:

php -v

The Curl utility is the simplest way to work with Curl from the command line, so it is a natural place to start when creating a PHP script. You will need to begin by initializing Curl:

$curl = curl_init();

Then pass Curl the URL you wish to retrieve:

curl_setopt($curl, CURLOPT_URL, 'http://127.0.0.1:5984/_all_dbs');

Next, perform actions on the results. In this case, we will retrieve a list of databases on the server, and print this list to the browser:

$db_list = curl_exec($curl);
echo $db_list;

Finally, close the Curl connection:

curl_close($curl);

The full script reads:

<?php

  // Initialize Curl
  $curl = curl_init();

  // Pass Curl the URL
  curl_setopt($curl, CURLOPT_URL, 'http://127.0.0.1:5984/_all_dbs');

  // Perform actions
  $db_list = curl_exec($curl);
  echo $db_list;

  // Close Curl
  curl_close($curl);

?>

To run a different query, change the URL. For example, to view the document we created in our example reviews database, substitute the following line in the script above:

curl_setopt($curl, CURLOPT_URL, 'http://127.0.0.1:5984/reviews/01');

The browser will display the content of the document in JSON format: {"_id":"01","_rev":"1-8ce1d23b7455705c3c2cbeeb86d8ccf5","reviewer_name":"Ben","stars":"5","details":"Love the calzone!"} 1