Sales Lead Tracking with PHP and MongoDB via MongoLab REST API
For my site offering hosting, support and consulting around Strider Continuous Integration & Deployment, I happened to be in just that situation. My web server is already configured for PHP and although I could spend the time to spin up a Python or Node.JS server, it adds needless complexity. All I wanted to do is one simple thing: track new sales leads in a MongoDB collection.
Lovely Free Cloud MongoDB from MongoLab
My web server doesn't run MongoDB, but it doesn't need to. MongoLab have a generous free plan which gives you a cloud-hosted MongoDB instance with up to 500M of storage. That is total overkill for my little sales lead database, and performance is more than adequate. Furthermore they will handle backup/restore and failover etc for me so it's likely a lot more reliable than running my own setup.
MongoDB REST API Benefits
Furthermore, MongoLab offer a HTTP REST API for MongoDB which is very fully-featured. Why would I want to use their REST API instead of a native driver? Well it turns out that my web server doesn't have the PHP MongoDB driver installed. It does however have the cURL library installed. Instead of bothering to figure out how to install the PHP MongoDB driver (which I suspect maybe be non-trivial under OpenBSD) I could just use their REST API.
Writing with MongoLab REST API from PHP
I didn't find much info online about using the REST API from PHP so I thought I'd post my little script here. It is quite straight forward. It simply JSON encodes some fields to produce the document I wish to write to my MongoDB collection. Then it sends a HTTP POST request with the JSON data as the POST body.
Niall O'Higgins is an author and software developer. He wrote the O'Reilly book MongoDB and Python. He also develops Strider Open Source Continuous Deployment and offers full-stack consulting services at FrozenRidge.co.Tweet