Build a PHP App that Uses GPS Data From an IoT Device

For many people, the Internet of Things is…well, the Next Big Thing. It’s all about using the cloud, real-time data, and new sensor technology to collect, store and analyze information from a wide range of previously-unwired sources: your house, your car, your refrigerator, your pet and so on.

While IoT is certainly very exciting, it’s hard to figure out exactly how to get started building IoT applications…especially if you’re a Web developer without a background in electrical engineering (like me). So I decided to invest a few weekends in learning about IoT and seeing if I could actually get a working IoT application up and running in PHP.

That’s where the Internet of Things Foundation (IoTF) service came in. A hosted cloud service that’s available through IBM Bluemix, IoTF makes it super-easy to aggregate real-time streams for devices and do something useful with them. After a little bit of research, I was able to successfully use it to retrieve GPS coordinates from my Android smartphone and then combine that data with the Google Maps API (and a little bit of PHP glue) to plot my phone’s location on a map in real time.

php-iotIt’s a simple application, but it goes a long way towards demonstrating the potential of IoT applications…and it’s also given me the foundation I need to begin building more complex applications. If this sounds like something you’d like to get in on as well, take a look at my IBM developerWorks article about using IoT with PHP, and also has some code for you to get started with.

Build and Deploy an Investment Tracking Application in the Cloud with IBM Bluemix

Financial data is now more accessible than ever via open APIs. This makes it easy to build Web or mobile applications that let you track your investment portfolio on a minute-by-minute basis (or at least as often as you care to check it). That’s the idea behind my new portfolio tracking application, which lets you create a virtual portfolio of stocks and then shows you the current market value at any given point.

How does it work? It’s mainly driven off the Quandl API, which is free to use and offers access to both open and premium financial databases. The Quandl API supports thousands of datasets, but I’m only using the free WIKI dataset, which is a community-curated list of end-of-day stock prices and dividends for 3,000 US companies.

The application itself is written in PHP using the Silex micro-framework and uses a hosted Cloudant instance for data storage. jQuery Mobile ensures that the interface works well on smartphones, and the entire thing is hosted on Bluemix for maximum scalability. There’s also a little bit of HybridAuth in the mix, just so that every user can create his or her own account in the system using existing Google credentials.

Sounds interesting? Try a demo, check out the source code on Github, or read about how I developed it in this two-part IBM developerWorks article.

Build a Photo Storage Service in the Cloud with PHP and IBM Bluemix

Cloud storage is increasingly popular because it’s cheap and scalable. Amazon Simple Storage Service (Amazon S3) is probably the most well-known, but almost every serious cloud vendor now has a storage offering. Cue my research into IBM Bluemix and its Object Storage service, which lets you attach cloud storage to your Bluemix application.

Bluemix’s Object Storage service is based on OpenStack Swift and follows Swift’s three-tier hierarchy for organizing data: accounts, containers and objects. To try it out, I built a photo storage application hosted on Bluemix and integrated it with the Bluemix Object Storage service. With a little help from the IBM jStart PHP client library, it was easy enough to implement single-user photo upload and storage. And because Bluemix’s Object Storage service supports multiple sub-accounts per service instance, it wasn’t too hard to extend the base application to support multiple users and multiple containers per user.

The final application uses Bootstrap for the user interface, Silex for application flow control, Twig for page templates and HybridAuth for user authentication. You can read the complete article here, get the source code from Github, or try out a live demo.

Debugging PHP Errors on IBM Cloud

I’ve been using IBM Cloud (aka IBM Bluemix) for various projects, but one of the things that often frustrates me is the lack of debug information available. For example, it’s quite common to upload a new PHP application to IBM Cloud and then see a blank page when attempting to access it.

The typical reason for this is that there’s a script error somewhere but the buildpack is configured to hide errors. Normally, the server error logs would let you figure out which script was causing the problem but since IBM Cloud is a PaaS, there’s no server console to log in to and tail log file output.

The best solution here is to tail recent log file output using the command below:

shell> cf logs <app_name>

You can also dump recent log file output using the command below:

shell> cf logs <app_name> --recent

As the image below illustrates, this usually has enough information to help you locate the source of the error, fix it and push a revised build to IBM Cloud.


Build and Deploy an ATM Finder App on Bluemix

Google makes a number of APIs available for free – I’ve previously written about the Google Tasks API and the Google+ API. I recently came across another interesting one – the Google Places API. As the name suggests, the Google Places API lets you retrieve information about shops, businesses, and other establishments by location or keyword. The information returned by the API is quite detailed – it includes place names, place types, coordinates, addresses, opening hours, all coupled with a unique place ID.

While experimenting with it, I thought of one useful application: finding the nearest ATM at any given location. It turned out to be reasonably easy to build, and things for even more interesting when I mashed up the ATM coordinates returned by the Google Places API with a map from the Google Maps API. The end result: a neat little mobile application that uses your current location to find a list of nearby ATMs, then shows you where they are on a map.

This mobile ATM finder is now live on IBM Bluemix. Try it for free from your smartphone, read about how I built it or download the code to see what makes it tick.

Host Your Application in the Cloud with XAMPP and Bitnami

If you’re a developer used to developing and testing applications locally, moving your code to the cloud can be a bit daunting. First, you have to choose a cloud from the numerous options availabe. Then, you need to deal with the nitty-gritties of pricing, provisioning, application deployment, scalability and security. And finally, you need to keep track of server performance, debug application issues and find ways to cost-efficiently improve performance.

Wish you had someone to hold your hand through the process? Read my detailed Apache Friends tutorials, which walk you through the process of hosting a PHP application in the cloud with Bitnami. In these tutorials, I describe how to use the Bitnami LAMP Stack to simply and efficiently create and provision a cloud server, then transfer your PHP application to it and monitor its performance.

The tutorial covers these these popular cloud hosts:

Take a look…you’ll be surprised by how easy it actually is!

Integrating PHP Web Applications with JCR and Magnolia CMS

I first came across PHPCR through my work with Magnolia CMS, an open source Java CMS. Magnolia CMS uses the Java Content Repository (JSR-283) as its content storage mechanism and PHPCR is a PHP adaptation of that standard. It’s great for storing semi-structured content in a hierarchical format and it also includes support for content versioning and indexing.

The cool thing about PHPCR is that it lets PHP developers interact with content stored in a JCR-compliant content repository using PHP objects and APIs…no Java knowledge needed. This means that you could save data to a JCR repository using a Java application, then read or modify that data using PHP, simplifying data reuse and interoperability between applications running on different platforms.

Read more in my tutorial on integrating PHP applications with Magnolia CMS. It discusses a number of common use cases, including reading and writing content to a JCR repository and integrating a PHP front-end with a Java application via the JCR.

Build and Deploy a Mobile-Friendly Calorie Counter on IBM Bluemix with PHP, MySQL, AngularJS and the Nutritionix API

The Nutritionix API is an online API which lets you access a massive database of nutrition information, covering both generic and branded food items. I came across it recently when looking for something else, and was promptly diverted from my original goal into trying it out.

While playing with it, it occurred to me that this could be the basis of a very neat little application to track calorie consumption. Ideally, it needed to work on a mobile device so that it would be at hand at meal-times, and be simple, interactive and easy to use. After a little thought, I had my requirements and design down, so I went ahead and built it.

Check out the video below to see it in action:

For the user interface, I used jQuery Mobile and AngularJS (something I’ve been itching to play with for a while). On the server, I used Slim, a PHP micro-framework, to connect with the Nutritionix API. The application is deployed on IBM Bluemix, which is also responsible for providing a MySQL service instance for account and meal record storage.

Although I’ve used the PHP/MySQL combination on IBM Bluemix before, this application gave me an opportunity to try something new: email send-outs. If you’re developing locally, or on a system you have administrative privileges for, it’s easy enough to configure a mail server to handle outgoing email traffic, then use PHP’s mail() function to do the deed. On a PaaS service, this isn’t quite as easy…but Bluemix’s SendGrid service provides a viable solution if you’re willing to experiment a bit.

For the full story, read my IBM developerWorks article…and then, don’t forget to try the demo app.

Build and Deploy a REST API on IBM Bluemix with PHP and MySQL

I’ve been experimenting with IBM Bluemix for some time now and a few weeks ago, I decided to try hosting a custom REST API with it. I built the API using Bullet, a PHP micro-framework, then deployed it to Bluemix and connected it with Bluemix’s MySQL service. It worked out pretty well…as you can see by trying this live demo.

In case you’re interested in learning how I went about building this, you can read all about it in this IBM developerWorks article. In this article, I explain how to implement the four basic REST methods using Bullet, then add support for API authentication and multiformat support, and finally deploy the API to Bluemix. Take a look, then download the API source and see what you think!

Configure URL Rewriting for Framework-Based PHP Applications on IBM Bluemix

A few weeks ago, I was introduced to Bluemix, IBM’s new cloud infrastructure platform for developers. With a bunch of pre-configured runtimes for Java, Node.js and Ruby, plus MongoDB, MySQL and various other database services, BlueMix provides a platform for quick app development and deployment.

Bluemix is currently in beta and doesn’t support PHP (yet) but I found a blog post which suggested it was possible using the Zend Server PHP buildpack. However, on trying it, I came across a problem: my Web application (built with Slim Framework) needed URL rewriting for its custom routes to work, but I couldn’t find a way to edit the nginx configuration file included with the Zend Server PHP buildpack. The search for an alternative led me (via Q&A in this thread) to the Cloud Foundry PHP buildpack, which solved my problem.

In case you’re looking to run a PHP application on Bluemix and you need URL rewriting (or other server-level modifications to configuration), here are the steps I followed:

1. Use the application manifest to load the Cloud Foundry PHP buildpack.
The application manifest file manifest.yml tells the Cloud Foundry CLI how to deploy the application, including which buildpack to use. Here’s what I used.


- name: my-slim-php-app
memory: 256M
instances: 1
host: my-slim-php-app

2. Override default buildpack settings with your custom configuration.
The best thing about the Cloud Foundry PHP buildpack is that it lets you override default httpd, nginx and PHP configuration settings with your own. All you need to do is add a .bp-config/ directory to your application’s root, then add an options.json file with buildpack configuration variables.

By default, the buildpack will install Apache as the Web server. To use nginx instead, I made some changes.


  "WEB_SERVER": "nginx"

The buildpack also lets you custom-configure Apache, nginx or PHP, simply by adding your custom configuration files within a httpd/, nginx/ or php/ sub-directory. By combining the URL rewriting instructions in Slim’s documentation with the existing nginx defaults, I generated the following overrides.


        listen @{VCAP_APP_PORT};
        server_name _;

        fastcgi_temp_path @{TMPDIR}/nginx_fastcgi 1 2;
        client_body_temp_path @{TMPDIR}/nginx_client_body 1 2;
        proxy_temp_path @{TMPDIR}/nginx_proxy 1 2;

        real_ip_header x-forwarded-for;
        real_ip_recursive on;

        # ?$args or ?$query_string at the end of the next line
        # tells nginx to forward query arguments
        try_files $uri /index.php?$query_string;


        # Some basic cache-control for static files to be sent to the browser
        location ~* \.(?:ico|css|js|gif|jpeg|jpg|png)$ {
            expires max;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";

        # Deny hidden files (.htaccess, .htpasswd, .DS_Store).
        location ~ /\. {
            deny all;
            access_log off;
            log_not_found off;

        # modified from Slim documentation
        # pass *.php to the fastcgi process  
        location ~ .*\.php$ {
            try_files $uri =404;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass php_fpm;

3. Push your application code to Bluemix.

shell> cf api
shell> cf login
shell> cf push

That’s pretty much all there is to it. You should now be able to access your Slim routes without any problems. These settings should also work (with minor changes) for other framework-based PHP applications, including those written with BulletPHP, Zend Framework and Agavi.

If you prefer Apache, this thread explains how to set up the necessary URL rewriting rules for Apache. If you don’t have a Bluemix account, you can request one here.