Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Sample PHP+Mongo app on Heroku

  1. Install any of the Mongo add-ons available at http://addons.heroku.com

  2. Vendor the Mongo driver with your application. You can download it here:

    https://github.com/wuputah/heroku-libraries/raw/master/php/mongo/mongo.so
    

    Add it to a folder like "ext".

  3. Add a php.ini file to the root of your application:

    extension_dir = "/app/www/ext/"
    extension=mongo.so
    
  4. Use it! Parse the db name out of the env var (keep in mind different Mongo providers will give you different urls, like MONGOHQ_URL or MONGOLAB_URL).

    Here's a sample index.php:

    <?php
      # get the mongo db name out of the env
      $mongo_url = parse_url(getenv("MONGO_URL"));
      $dbname = str_replace("/", "", $mongo_url["path"]);
    
      # connect
      $m   = new Mongo(getenv("MONGO_URL"));
      $db  = $m->$dbname;
      $col = $db->access;
    
      # insert a document
      $visit = array( "ip" => $_SERVER["HTTP_X_FORWARDED_FOR"] );
      $col->insert($visit);
    
      # print all existing documents
      $data = $col->find();
      foreach($data as $visit) {
        echo "<li>" . $visit["ip"] . "</li>";
      }
    
      # disconnect
      $m->close();
    ?>
    
@robbiet480

This comment has been minimized.

Copy link

robbiet480 commented May 17, 2012

The correct env vars now end with URI, not URL

@NMPPaul

This comment has been minimized.

Copy link

NMPPaul commented Feb 25, 2013

These instructions no longer work!

There's an unanswered Stack Overflow thread about it here: http://stackoverflow.com/questions/14941515/php-heroku-app-deployment-using-mongo

Can anyone help?

@rwarasaurus

This comment has been minimized.

Copy link

rwarasaurus commented Mar 22, 2013

Its now MONGOHQ_URL !

@nishantmendiratta

This comment has been minimized.

Copy link

nishantmendiratta commented Oct 25, 2014

I have tried follwoing steps and ##GETTING ERROR -
Fatal error: Class 'Mongo' not found in /app/mongo.php on line 13

  1. Installed MongoHQ add-on in heroku app.

  2. Downloaded - https://github.com/wuputah/heroku-libraries/raw/master/php/mongo/mongo.so driver and then created "ext" folder in root directory and saved mongo.so in that dir.

  3. Added php.ini file in my root directory with following code

      extension_dir = "/app/www/ext/"
      extension=mongo.so 
    

Uploaded following code on server named mongo.php file

<?php
ini_set("display_errors", 1);
try {
// connect to Compose assuming your MONGOHQ_URL environment
// variable contains the connection string
$connection_url = getenv("MONGOHQ_URL");

 // create the mongo connection object
$m = new Mongo($connection_url);

// extract the DB name from the connection path
$url = parse_url($connection_url);
$db_name = preg_replace('/\/(.*)/', '$1', $url['path']);

// use the database we connected to
$db = $m->selectDB($db_name);

echo "<h2>Collections</h2>";
echo "<ul>";

// print out list of collections
$cursor = $db->listCollections();
$collection_name = "";
foreach( $cursor as $doc ) {
  echo "<li>" .  $doc->getName() . "</li>";
  $collection_name = $doc->getName();
}
echo "</ul>";

// print out last collection
if ( $collection_name != "" ) {
  $collection = $db->selectCollection($collection_name);
  echo "<h2>Documents in ${collection_name}</h2>";

  // only print out the first 5 docs
  $cursor = $collection->find();
  $cursor->limit(5);
  echo $cursor->count() . ' document(s) found. <br/>';
  foreach( $cursor as $doc ) {
    echo "<pre>";
    var_dump($doc);
    echo "</pre>";
  }
}
// disconnect from server
$m->close();
 } catch ( MongoConnectionException $e ) {
die('Error connecting to MongoDB server');
 } catch ( MongoException $e ) {
die('Mongo Error: ' . $e->getMessage());
 } catch ( Exception $e ) {
die('Error: ' . $e->getMessage());
 }
 ?>

GETTING ERROR -

Fatal error: Class 'Mongo' not found in /app/mongo.php on line 13

@nishantmendiratta

This comment has been minimized.

Copy link

nishantmendiratta commented Oct 26, 2014

--Issue resolved by adding compose.json in root directory with following code

   {
     "require": {
         "ext-mongo": "*"
      }
   }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.