Five-Minute RESTful Backend

Need to persist some data, quick and dirty, for a prototype? If you’re up for ignoring security and performance considerations, I definitely recommend a MongoDB + PHP backend. You can get the whole thing running in five minutes, and you’ll be free to play around with the JavaScript. That’s what you were after from the beginning, right?

1. Sign up for a free MongoLab account, then set up a database and collection. They have a great RESTful API that’ll prevent you from having to set up any actual Mongo drivers. This makes it easy if you’re on a shared host.

2. Include a little PHP script like this one. This script will handle both GET and POST requests. For GET requests it will look up a document (or documents) in a hard-coded database and collection based on a matching “url” key, supplied through the query string. For POST requests, it posts a single sample value called “value” into the same database and collection.

<?php
// Get this at your MongoLab.com user page
$MONGOLAB_API_KEY = '(Your Key Here)';
$DB = '(database name)';
$COLLECTION = '(collection name)';
$url = "https://api.mongolab.com/api/1/databases/$DB/collections/$COLLECTION?apiKey=$MONGOLAB_API_KEY";

$method = $_SERVER['REQUEST_METHOD'];

switch ($method) {
    case 'POST':
        request_post();
        break;
    case 'GET':
        request_get();
        break;
}

function request_get()
{
    // As an example, we'll pass the parameter supplied by the user straight to the Mongo Query. This is not secure!
    header('Content-Type: application/json');
    global $url;
    $url = $url ."&q=". urlencode ( '{"url": "'.$_GET['url'].'"}' );

    try {

      $ch = curl_init();

      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

      $response = curl_exec($ch);
      $error = curl_error($ch);
      $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      curl_close($ch);

    } catch (Exception $e) {
      echo "FAIL";
    }
}

function request_post()
{
    global $url;

    // Post some user-supplied stuff to the database. Again, you'll want to verify the data and add security restrictions here!
    $value = preg_replace("/[^A-Za-z0-9\-]/", '', $_POST['value']);

    $data = json_encode(
      array(
        "value" => $value
      )
    );

    try {
      $ch = curl_init();

      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
      curl_setopt($ch, CURLOPT_HTTPHEADER, array(
          'Content-Type: application/json',
          'Content-Length: ' . strlen($data),
          )
      );

      $response = curl_exec($ch);
      $error = curl_error($ch);
      $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      curl_close($ch);

      echo "SAVED";
    } catch (Exception $e) {
      echo "FAIL";
    }
}
?>

3. Start storing and retrieving your data!