Introduction
The article intends to demonstrate PHP support in Google Cloud which has been recently announced on an experimental basis. I thank Google for allowing me access to this technology stack which is not yet publicly available on Google App Engine. I quickly cobbled up this article over a Sunday to help technologists wanting to use PHP on Google Cloud (Google App Engine) which is an elegant & reliable PaaS to host WebApps. Some experience with Google Cloud shall help but is not mandatory.
Background
Over the past decade we have had newer trends overwhelming the Web technologies every now and then. PHP is one technology which has stood the test of time. Although, it may seem that PHP is losing its midas touch, with newer stuff like Node.js and Ruby making steady headway, it still is huge. That is exactly the reason why Google, Microsoft have had to support PHP on their cloud offerings. Microsoft realized this sooner when Azure was still in Beta. I had even written a simple custom PHP blog engine running on Microsoft Azure. (This was the winner in PHP Category, within the thecloudapp.com contest organised by Microsoft India in 2009).
Google Cloud (PaaS) however, initially stuck to Python, followed by Java and Go languages. Recently, Google announced experimental support for PHP. For me its - better late than never. This presents a awesome opportunity for PHP enthusiasts and could be a game changer for Google Cloud. I am sure Google shall open this up to everyone sooner rather than later. Currently, Google might allow you to enable your PHP applications to be deployed to App Engine, if you request here.
Step 1: Setup
One of the first steps to is to setup your development environment up for Google Cloud. Simply download a& install Google App Engine SDK & PHP (In my case GoogleAppEngine-1.8.3.msi and php-5.4.17-Win32-VC9-x86.zip). If you use Java for GAE it may help to use Eclipse as IDE, but is not mandatory in this case. Relevant instructions for your OS are available here.
Step 2: Create
Lack of clear documentation makes creation of application a little tricky area. First step is to create a python Google Cloud app or simply take the sample app from the SDK. This contains a configuration file app.yaml. We just need to change few lines to make it a php web app.
1. Replace python with php.
runtime: php
2. Replace handler for php files. Note how static files need to be specified.
handlers:
- url: /css
static_dir: css
- url: /js
static_dir: js
- url: /fonts
static_dir: fonts
- url: /assets
static_dir: assets
- url: /shortener
script: shortener.php
- url: /phpinfo
script: phpinfo.php
- url: /.*
script: index.php
3. Create php.ini file in the root folder for some restricted functions you may want to use.
; php.ini
google_app_engine.enable_functions = "php_sapi_name, php_uname, phpinfo"
Step 3: Implement
Code your php web app. In this case I wrote a simple PHP Web App based on Twitter BootStrap 3.0 demonstrating Google API calls from PHP. Although I would not use Bootstrap in this way in production it was to quickest way to get a responsive UI based on HTML5 and JQuery which can be served across PC, Tablets and Mobiles in an aesthetic manner. Note few things would not work on Google Cloud e.g functions like phpinfo();, file system access etc. However, one if free to use Google Cloud Storage. The code for this PHP Web App is available for download.
This how my Web App looks on desktop
This how the same Web App looks on mobile (Thanks to responsive HTML5 from Twitter BootStrap!)
Step 4: Call Google API
Currently the demo calls Google's awesome URL Shortening API. PHP code used could'nt be any simpler. To be able to use Google APIs you need to download and use Google PHP Client Library from here. However a major difference exists between normal use and on Google Cloud, as we cannot use FileSystem Cache and and CURL on Google Cloud. To be able to use Google APIs on you need change your following lines in google-api-php-client\src\config.php. (This would have been difficult to debug without this blog - Many Thanks!)
'authClass' => 'Google_OAuth2',
'ioClass' => 'Google_HttpStreamIO',
'cacheClass' => 'Google_MemcacheCache',
'ioMemCacheCache_host' => 'does_not_matter',
'ioMemCacheCache_port' => '37337',
This is particularly troublesome as I did not memcache on my filesystem I had to switch between 'cacheClass' => 'Google_FileCache'
(Local Testing) and 'cacheClass' => 'Google_MemcacheCache'
(Google Cloud Production) .
URL Shortening Logic is as simple as
$client = new Google_Client();
$service = new Google_UrlshortenerService($client);
if (isset($_GET['url']))
{
$url = new Google_Url();
$url->longUrl = $_GET['url'];
$shortUrl = $service->url->insert($url);
}
The "Hello World" of PHP Kingdom is as simple as
<?php
phpinfo();
?>
Running the Web App
Running Web App Locally is pretty easy. Run this command from out side root folder (php-4-gae in this case)
C:\eclipse\workspace>dev_appserver.py --php_executable_path=C:\php\php-cgi.exe php-4-gae
For uploading to Google Cloud run this command (You may be prompted for login id and password).
C:\eclipse\workspace>appcfg.py update php-4-gae
Notes
- 31 Aug 2013: Uploaded first draft over a weekend.
If I get some spare time over next weekend, I hope to publish next article using some popular framework like CodeIgnitor based REST API built on PHP and Google Cloud.
And may be add some more Google API calls in the demo.