Category Archives: appengine

Making Cloud Datastore Easy For PHP on App Engine

Firstly, thank you to @slangley and the team for asking me to guest post here. I really hope this helps you PHP folks on App Engine.

TL;DR – I can has code?

Sure, no problem. https://github.com/tomwalder/php-gds

Introduction

PHP on Google App Engine went Generally Available earlier this year, and that’s pretty awesome.

Certainly for me (and probably some of you) it makes both prototyping and running super-scalable web applications really easy and reliable.

Whilst the PHP part of the App Engine environment is ultra easy to use and scalable, you are likely to need a similarly featured data persistence layer for any useful applications. MySQL is cool and all, but it costs money (via Cloud SQL) and can’t scale automatically in the same way App Engine can.

Enter Cloud Datastore.

It’s pretty sweet – auto-scaling, distributed, supports transactions, mostly schema-less, managed, free to get started (up to 1GB of data and 50k queries per day).

So, you ask yourself, how do I use this amazing thing? Well until not that long ago it was a bit of a pain. you needed loads of glue code to get even the basics running.

So the php-gds library was built to simplify usage of Datastore from PHP. It tries to make it really, really easy to get started, whilst also being feature rich. It’s seeing a fair amount of adoption as far as I can tell, and the guys at Google are pretty keen on it too – hence this post!

Example – Write to Datastore

For any code running on the local development server or on a real App Engine application, it’s this easy…

// Build a new entity
$obj_book = new GDS\Entity();
$obj_book->title = 'Romeo and Juliet';
$obj_book->author = 'William Shakespeare';
$obj_book->isbn = '1840224339';

// Write it to Datastore
$obj_store = new GDS\Store('Book');
$obj_store->upsert($obj_book);

Example – Read from Datastore

Again, I try to make reading data is simple and intuitive as possible…

$obj_store = new GDS\Store('Book');
foreach($obj_store->fetchAll() as $obj_book) {
    echo "Title: {$obj_book->title}, ISBN: {$obj_book->isbn} <br />", PHP_EOL;
}

Native Access or JSON API

The php-gds library uses the native, Protocol Buffer APIs to access the Datastore – this means it is super fast.

It also supports remote access using the JSON API – in fact, you can use the same code against both, just passing the right Gateway object into your Store. More details on GitHub here.

Query Language – GQL

At the time of writing, php-gds uses the GQL, which is very similar to SQL, to query Datastore – and parameter binding, which is recommended.

$obj_book_store->fetchOne("SELECT * FROM Book WHERE isbn = @isbnNumber", [
    'isbnNumber' => '1853260304'
]);

Best Practice – Define a Schema/Model

You will get more (for less) if you define your Schema clearly, including what fields in your data to index. Indexing costs money (above the free quota) so it’s a good move to think about and define your model

More detail on defining a model can be found here.

Further Reading

There is a pretty decent set of guidance in the GitHub README and in the examples.

Support

Create an issue on GitHub or post on SO.

About Me

My name is Tom Walder, I’m CTO at Docnet, an e-commerce platform and solutions provider based in Manchester, UK. You can find me on twitter @tom_walder

I am the author of the php-gds Datastore library for PHP.

Direct File Uploads for PHP 5.5

One of the new features we announced in the 1.9.18 App Engine release is the ability to upload files directly to you application, without the need to upload the files to Google Cloud Storage first.

Direct uploads leverages the same in memory virtual filesystem that is used to provide temporary filesystem support. Direct uploads are only available with the PHP 5.5 runtime and are also limited to a maximum combined size of 32MB, which is the incoming request size limit.

Direct Upload Example

Following is a small sample application that demonstrates direct file uploads. This sample application:

  • Uses HTML5 multiple file upload support to upload image files.
  • Saves the original uploaded file into Google Cloud Storage.
  • Creates a greyscale version of the uploaded image and writes that to Google Cloud Storage.
  • Uses the image serving API to create links to serve the stored images at original size.
  • Also uses the image serving API to create thumbnail links of the images, without the application having to create the thumbnails.
  • Finally, displays a simple page with the thunbnailed links to the saved files.

For this application we’ll create 3 source files.

app.yaml

The app.yaml file has the following contents. Note that the runtime is php55.

application: php-uploads
version: 1
runtime: php55
api_version: 1
threadsafe: true

handlers:
- url: /handle_upload
  script: handle_upload.php

- url: .*
  script: direct_upload.php

direct_upload.php

This file presents a form for the user to upload multiple files to your application. As direct uploads are only in PHP 55 it checks that as well.

<?php
// Direct uploads requires PHP 5.5 on App Engine.
if (strncmp("5.5", phpversion(), strlen("5.5")) != 0) {
  die("Direct uploads require the PHP 5.5 runtime. Your runtime: " . phpversion());
}
?>
<html>
<body>
<form action="handle_upload" method="post" enctype="multipart/form-data">
  Send these files:<p/>
  <input name="userfile[]" type="file" multiple="multiple"/><p/>
  <input type="submit" value="Send files" />
</form>
</body>
</html>

handle_upload.php

The handle_upload script does the heavy lifting of creating the greyscale images, saving them in Cloud Storage and displaying the results. If you were doing this in a production app you’d need a lot more error handling, but I’ve left it out for brevity.

<?php

use google\appengine\api\cloud_storage\CloudStorageTools;

$bucket = CloudStorageTools::getDefaultGoogleStorageBucketName();
$root_path = 'gs://' . $bucket . '/' . $_SERVER["REQUEST_ID_HASH"] . '/';

$public_urls = [];
foreach($_FILES['userfile']['name'] as $idx => $name) {
  if ($_FILES['userfile']['type'][$idx] === 'image/jpeg') {
    $im = imagecreatefromjpeg($_FILES['userfile']['tmp_name'][$idx]);
    imagefilter($im, IMG_FILTER_GRAYSCALE);
    $grayscale = $root_path .  'gray/' . $name;
    imagejpeg($im, $grayscale);

    $original = $root_path . 'original/' . $name;
    move_uploaded_file($_FILES['userfile']['tmp_name'][$idx], $original);

    $public_urls[] = [
        'name' => $name,
        'original' => CloudStorageTools::getImageServingUrl($original),
        'original_thumb' => CloudStorageTools::getImageServingUrl($original, ['size' => 75]),
        'grayscale' => CloudStorageTools::getImageServingUrl($grayscale),
        'grayscale_thumb' => CloudStorageTools::getImageServingUrl($grayscale, ['size' => 75]),
    ];
  } 
}

?>
<html>
<body>
<?php
foreach($public_urls as $urls) {
  echo '<a href="' . $urls['original'] .'"><IMG src="' . $urls['original_thumb'] .'"></a> ';
  echo '<a href="' . $urls['grayscale'] .'"><IMG src="' . $urls['grayscale_thumb'] .'"></a>';
  echo '<p>';
}
?>
<p>
<a href="/">Upload More</a>
</body>
</html>

If you stumble across any problems, please let us know on either stack overflow or by creating an issue in our tracker.

gae_php

App Engine PHP 1.9.17 pre-release SDK

Well it’s been a while since the PHP team have had any updates to post to this blog. For the last few months the team has been heads down working with the rest of the App Engine team working on improving the reliability, stability and performance of the platform. You can read a little more on this in this post that our very own VP Daniel Sturman made on the App Engine discussion forum.

That being said, we’re ready to get the ball rolling once more on delivering a great PHP experience on App Engine, moving the platform from experimental to GA as soon as possible. In fact, we have the following features ready to launch in the next App Engine release, 1.9.17.

  • Upgrading the interpreter to PHP 5.5.18, which includes
    • support for Zend opcache.
    • APCu extension for in process caching.
    • backend improvements for parallel request serving from the same runtime instance.
  • Adding support for the cURL extension.
  • Adding support for the mailparse extension.

Now usually we do not pre-announce features that are scheduled in an upcoming release, because plans can change at the last minute and features can be pushed back a release or two (which could still happen here). But we regard this is a special case as the change from PHP 5.4.32 to PHP 5.5.18 could break existing applications. There is a small list of PHP 5.5 backwards incompatible changes and we want to give you developers a chance to fix any issues that your apps might have because of the switch.

To assist in identifying any potential problems in your applications, we’ve created 1.9.17 pre-release SDKs that have the PHP 5.5 interpreter bundled. You can download these SDKs from the following links.

For linux users, we suggest building the interpreter from source, using the following shell commands.

  sudo apt-get install gcc libmysqlclient-dev libxml2-dev libcurl4-openssl-dev libpng-dev libjpeg-dev
  wget --trust-server-names http://us2.php.net/get/php-5.5.18.tar.bz2/from/us1.php.net/mirror
  tar -xvf php-5.5.18.tar.bz2
  cd php-5.5.18
  DEST=$HOME/app_engine/5.5
  ./configure --prefix=$DEST/installdir \
    --enable-bcmath \
    --enable-calendar \
    --enable-ftp \
    --enable-mbstring \
    --enable-opcache \
    --enable-soap \
    --enable-sockets \
    --enable-zip \
    --disable-fileinfo \
    --disable-flatfile \
    --disable-posix \
    --with-curl \
    --with-gd \
    --with-openssl \
    --without-sqlite3 \
    --without-pdo-sqlite \
    --without-imap \
    --without-kerberos \
    --without-imap-ssl\
    --without-interbase \
    --without-ldap \
    --without-mssql \
    --without-oci8 \
    --without-pgsql \
    --without-pear \
    --disable-phar \
    --without-snmp \
    --enable-mysqlnd \
    --with-pdo-mysql=mysqlnd \
    --with-mysqli=mysqlnd \
    --with-mysql=mysqlnd
  make install -j
  cd -

This will build an php-cgi binary that is used by the App Engine development server, which can be found located in the folder $DEST/app_engine/5.5/installdir/bin. Follow these instructions to run the development appserver with the php-cgi binary that you’ve built.

As always, you can report any issues in the public issue tracker and we’ll do our best to solve them promptly.

banner-772x250

AppEngine WordPress plugin version 1.4 released.

We’re happy to announce the latest release of the AppEngine WordPress plugin, version 1.4. We encourage all users of WordPress on App Engine to upgrade to this new version as soon as possible.

This new versions includes a number of bug fixes, as well as a new feature for serving media files over HTTPs.

Bug Fixes

This version of the plugin includes fixes for the following bugs:

  • The default HTTP fetch timeout is now 30 seconds, and fetch errors include a more descriptive message.
  • The public URLs for uploaded media now work correctly in the development environment.
  • The plugin now uses autoloading in the AppEngine SDK to improve load time performance.

New Features

This version of the plugin includes a new feature, that makes it possible to serve uploaded media files over HTTPs or HTTP.

To enable HTTPs serving of uploaded media, select the checkbox in the plugin settings page, as shown below.

Secure URLs checkbox.

Once selected, all media files that are subsequently uploaded will be served over https. Note: Changing this setting has no affect on media files that have already been uploaded.

Please report any issues with the plugin in the support forum, or ask questions on stackoverflow where members of the team actively participate.

We also gladly accept pull requests – you can find the plugin source on Github.

Using the WordPress Importer From the App Engine Plugin

As we announced recently, the new version of our App Engine WordPress Plugin includes import support for WordPress export files, forked from the popular WordPress importer plugin.
This plugin lets you take content that you’ve exported from another WordPress site, in the form of an .xml file, and import it into your App Engine WordPress site.

This post walks through the process of doing such an import. It assumes that you already have a WordPress installation on App Engine— if not, see these instructions.

If you will be doing an import of a large .xml file, see the section below on “What to Do if You Have a Large Import“.  As described in that section, you should make a temporary configuration change to your app before you do the import, to ensure that it finishes successfully.

Continue reading

banner-772x250

AppEngine WordPress plugin Version 1.3 Released

We’re happy to announce that we’ve just released version 1.3 of the AppEngine WordPress plugin. We encourage all users of WordPress on App Engine to upgrade to this new version as soon as possible.

The new version of the plugin includes the following features and bug fixes.

  • Import support for WordPress export files, forked from the popular WordPress importer plugin.
  • URLFetch support for WordPress versions 3.7 and above, that fixes issues caused by core WordPress changes to the WT_HTTP Class.
  • Bug fix for incorrectly detecting if a Google Cloud Storage bucket was writable by the application during setup.
  • Better logging of plugin errors.

Like always, please report any issues with the plugin in the support forum.

We also gladly accept pull requests – you can find the plugin source on Github.

PHP App Engine Apps and File System Concepts

If you’re new to App Engine, then the file system model may be a bit different from what you might have experienced using other hosts.

With an App Engine application, you cannot write to the file-system where your application is deployed. Your application can read any files from the deployed directory structure, but it can’t write to that file-system. Instead, the application can use Google Cloud Storage (GCS) for both reading and writing files.
To convert an app to use GCS for writable files, here are the primary things that you will typically need to do:

Another implication of the read-only file system is that if your app has a ‘plugin’ model, like WordPress, you can’t install or update plugins from your deployed app. Instead, you will need to install or update any plugins locally, then redeploy the app.

You can find lots more info on all of the following in the documentation for the PHP runtime.

Continue reading

App Engine WordPress plugin Version 1.2 released

We’re happy to announce that we’ve just released version 1.2 of the App Engine WordPress plugin. The new version includes the following changes

  • Fix for the bug “Uncaught exception ‘InvalidArgumentException’ with message ‘max_bytes_per_blob must be an integer'” (forum link)
  • Removes the need for PIL to be installed in the local development environment.
  • Removes the need for PyCrypto to be installed in the local development environment.
  • Better checking if the application has write access to the Google Cloud Storage bucket for uploads.

If you find any issues with the latest version of the plugin you can file them in the support forum.

We also gladly accept pull requests – you can find the plugin source on Github.

Generating Dynamic WordPress sitemaps on App Engine

Sitemaps are a valuable tool in helping search engines crawl your website. In this post I’ll show you how you can have a dynamically generated sitemap for a WordPress blog hosted on Google App Engine.

Because of the read-only nature of the file system on App Engine, we’ll use Google Cloud Storage to store the generated sitemap file. We’ll also add some handlers to serve the sitemap, and to update it as part of a scheduled cron job.

Continue reading

Getting started with the Cloud Datastore on PHP App Engine, Part II

Introduction

This is the second in a series of posts on using the Google Cloud Datastore on PHP App Engine.
The code for the examples in this post is here: https://github.com/amygdala/appengine_php_datastore_example.

The previous post walked through the process of setting up your PHP App Engine app to connect with the Datastore, using the Google API client libraries.
It demonstrated how to create a test Datastore entity, and showed how to set up a “Datastore service” instance, using a DatastoreService class, to make it easier to send requests.

However, using the API client libraries, there is a fair bit of overhead in composing requests to the Datastore and processing the responses, and mapping entities to instances of classes in your app. So, in this post, we’ll define some classes to make it easier to create, update, and fetch Datastore entities.
We’ll define a `Model` base class, then show an example of subclassing it to map to a specific Datastore Kind.

Continue reading