With App Engine, you can build web applications using the PHP Programming Language. Your PHP application runs on Google's scalable infrastructure and uses large- scale persistent storage and services.
See the PHP Getting Started Guide for an introduction to developing web applications with PHP and Google App Engine.
Selecting the PHP runtime
To set your app to use the PHP runtime, add the following to your
runtime: php55 api_version: 1 ...
The first element,
runtime, selects the PHP runtime environment.
The second element,
api_version, selects which version of the PHP runtime
environment to use. As of this writing, App Engine only has one version of the
1. If there are future changes that might not be backwards
compatible, the App Engine team will use a a new version identifier. Your app
will continue to use the selected version until you change the
setting and upload your app.
To allow App Engine to distribute requests for applications across multiple web servers, and to prevent one application from interfering with another, the application runs in a restricted "sandbox" environment. In this environment, the application can execute code, use the App Engine mail, URL fetch and users services, and examine the user's web request and prepare the response.
An App Engine application cannot:
write to the filesystem. PHP applications can use Google Cloud Storage for storing persistent files. Reading from the filesystem is allowed, and all application files uploaded with the application are available.
respond slowly. A web request to an application must be handled within a few seconds. Processes that take a very long time to respond are terminated to avoid overloading the web server.
make other kinds of system calls.
Automatic class loading
Both Standard PHP Library
(SPL) classes and any
classes that are part of the SDK for App Engine are automatically loaded when
needed. This means that you do not have to use
at the top of your PHP scripts.
By default, automatic class loading will occur only for classes defined in files
that reside in the root directory of SDK for App Engine root (and, if it has
been specified by
--php_executable_path, your local PHP installation).
To add more paths to be searched for automatic class loading, use
set_include_path in your PHP script.
set_include_path('my_additional_path' . PATH_SEPARATOR . get_include_path());
The following extensions have been enabled in the PHP runtime for App Engine:
- sockets (for billing-enabled apps)
Dynamically loadable extensions
The following extensions are dynamically loadable by configuring
- cURL - This extension uses the socket service to make requests, and is subject to that services' quota and restrictions, and is only available for applications with billing enabled. See Outbound Requests for more information.
- MongoDB - This extension allows a developer to connect to an existing MongoDB instance. It uses the socket service to make requests, and is subject to that services' quota and restrictions, and is only available for applications with billing enabled.
To enable these extensions, add directives for them in your
extension like so:
extension = "curl.so" extension = "mongo.so" extension = "imagick.so" extension = "intl.so" extension = "fileinfo.so"
Most web applications need a way to preserve user state information between requests. PHP provides a convenient session management layer. Sessions in App Engine work much like sessions in any other PHP application.
Setting a variable in a user's session:
session_start(); $_SESSION['Foo'] = 'Bar';
On a subsequent request by the same user:
session_start(); print $_SESSION['Foo']; // prints Bar
By default the App Engine runtime will use memcache to store session information
class. You can adjust this behavior by specifying your own session handler using
method. Memcache allows session data to be saved and retrieved quickly, meaning
the overhead on your request is minimal. However data in App Engine memcache
might be flushed periodically, meaning any session information will be lost. For
longer-lived sessions, it can be preferable to use an alternative storage
service such as Cloud SQL.
Special $_SERVER keys
PHP makes the special
$_SERVER array available to in the request scope. In
addition to the standard CGI paramaters, App Engine adds some additional useful
APPLICATION_ID- The app_id of the application set when the app was created. eg. my-wordpress
AUTH_DOMAIN- The domain used for authenticating users with the Users API. Apps hosted on appspot.com have an AUTH_DOMAIN of gmail.com, and accept any Google account. Apps hosted on a custom domain using Google Apps have an AUTH_DOMAIN equal to the custom domain
CURRENT_VERSION_ID- The major and minor version of the currently running application, as "X.Y". The major version number ("X") is specified in the app's app.yaml file. The minor version number ("Y") is set automatically when each version of the app is uploaded to App Engine. On the development web server, the minor version is always "1".
DEFAULT_VERSION_HOSTNAME- The hostname of the default version of this application, eg. my-php-app.appspot.com.
HTTP_X_APPENGINE_CITY- Name of the city from which the request originated. For example, a request from the city of Mountain View might have the header value mountain view.
HTTP_X_APPENGINE_CITYLATLONG- Latitude and longitude of the city from which the request originated. This string might look like "37.386051,-122.083851" for a request from Mountain View.
HTTP_X_APPENGINE_COUNTRY- Country from which the request originated, as an ISO 3166-1 alpha-2 country code. App Engine determines this code from the client's IP address.
HTTP_X_APPENGINE_REGION- Name of region from which the request originated. This value only makes sense in the context of the country in
X-AppEngine-Country. For example, if the country is "US" and the region is "ca", that "ca" means "California", not Canada.
USER_EMAIL- Returns the email address of the user, if they have been authenticated using the Users API. Applications should use nickname for displayable names.
USER_ID- If the email address is associated with a Google account, user_id returns the unique permanent ID of the user, a str. If they have been authenticated using the Users API. This ID is always the same for the user regardless of whether the user changes her email address.
USER_IS_ADMIN- 1 if the logged in user is also an Adminstrator of the application, if they have been authenticated using the Users API. 0 otherwise.
USER_NICKNAME- For Google Accounts users, the nickname is either the "name" portion of the user's email address if the address is in the same domain as the application, or the user's full email address otherwise.
USER_ORGANIZATION- An application using the Google Accounts setting can determine if the currently signed-in user is using a personal Google Account or an account which is managed by a Google Apps domain.
SCRIPT_NAME behavior in 1.9.0
The implementation of
to 1.9.0 differs significantly from 1.9.0 and onward. The changes were made to
be consistent with the Apache implementation generally expected by PHP
The following examples demonstrate the difference.
|Before 1.9.0||After 1.9.0|
- url: /.* script: index.php
REQUEST_URI: / SCRIPT_FILENAME: /path/to/index.php SCRIPT_NAME: / PHP_SELF: /
REQUEST_URI: / SCRIPT_FILENAME: /path/to/index.php SCRIPT_NAME: /index.php PHP_SELF: /index.php
REQUEST_URI: /bar SCRIPT_FILENAME: /path/to/index.php SCRIPT_NAME: /bar PHP_SELF: /bar
REQUEST_URI: /bar SCRIPT_FILENAME: /path/to/index.php SCRIPT_NAME: /index.php PHP_SELF: /index.php
REQUEST_URI: /index.php/foo/bar SCRIPT_FILENAME: /path/to/index.php SCRIPT_NAME: /index.php/foo/bar PHP_SELF: /index.php/foo/bar
REQUEST_URI: /index.php/foo/bar SCRIPT_FILENAME: /path/to/index.php SCRIPT_NAME: /index.php PHP_SELF: /index.php/foo/bar
REQUEST_URI: /test.php/foo/bar SCRIPT_FILENAME: /path/to/index.php SCRIPT_NAME: /test.php/foo/bar PHP_SELF: /test.php/foo/bar
REQUEST_URI: /test.php/foo/bar SCRIPT_FILENAME: /path/to/index.php SCRIPT_NAME: /index.php PHP_SELF: /index.php
- url: /.* script: foo/index.php
REQUEST_URI: /bar SCRIPT_FILENAME: /path/to/foo/index.php SCRIPT_NAME: /bar PHP_SELF: /bar
REQUEST_URI: /bar SCRIPT_FILENAME: /path/to/foo/index.php SCRIPT_NAME: /foo/index.php PHP_SELF: /foo/index.php
Directives with new initialization defaults
This table specifies directives whose initialization defaults differ from the defaults supplied with the standard PHP interpreter available from php.net. You can override these default directives by including them in a php.ini file for your application.
|Directive||Default Value in Google App Engine|
Either for security reasons, or for compatibility with Google App Engine execution environment, some PHP functions have been disabled. Some of these functions can be explicitly re-enabled in the php.ini file for your application.
Permanently disabled functions
The following functions have been permanently disabled in Google App Engine:
escapeshellarg() and escapeshellcmd()
lchgrp(), lchown(), link(), and symlink()
pclose() and popen()
proc_close(), prog_get_status(), proc_nice(), proc_open(), and proc_terminate()
Google App Engine does not include the
extension, and thus the functions provided by
pcntl are not
available to PHP apps running in App Engine.
App Engine apps run in a security sandbox that doesn't allow for writing to
local filesystem. For this reason, App Engine's version of
tempnam() returns an
in-memory temp file that can be written to permanent storage solution such as
Google Cloud Storage buckets later.
Here is an example on how to write to the in-memory temp file using
<?php $dir = sys_get_temp_dir(); $tmp = tempnam($dir, “foo”); file_put_contents($tmp, “hello”) $f = fopen($tmp, “a”); fwrite($f, “ world”); fclose($f) echo file_get_contents($tmp);
The expected output from the example would then be:
Partially restricted functions
The Google App Engine for PHP runtime does not support the
modifier of the
mb_ereg_replace() functions. See the
PREG_REPLACE_EVAL documentation for the deprecation
notice and an example of how to update your code to use
Functions that can be manually enabled
This list specifies the PHP function that must be manually enabled by using the
google_app_engine.enable_functions directive in the
php.ini file for your application.
Functions that require billing enabled
The following functions make use of Sockets and thus are only available to billing enabled apps.
- all Network Functions except
- all Sockets Functions
- all FTP functions
- cURL, unless using cURL Lite
Supported PHP I/O stream wrappers
The following PHP I/O stream wrappers are supported:
Many functions in PHP such as
file_get_contents() take advantage of PHP's streams interface to support different protocols.
The following is a list of built-in stream wrappers that are automatically registered and available in the App Engine runtime.
http://(This behaves like PHP's built-in http stream handler, but uses the App Engine URLfetch service)
https://(This uses the App Engine URLfetch service)
gs://(The stream handler for Google Cloud Storage)
The following is a list of built-in stream handlers that are not supported in Google App Engine and have been unregistered.
Disabled stream transports
The following stream transports have been disabled.
All code for the PHP runtime environment must be pure PHP. App Engine does not allow you to upload your own C extensions.
The environment includes the PHP standard library. Some extensions have been disabled because their core functions are not supported by App Engine, such as networking and writing to the filesystem.
You can include other pure PHP libraries with your application by putting the
code in your application's directory, which is the same directory that contains
For example, you can create a symbolic link in your application's directory that points to a library's directory. That link is then followed and that library gets included in your application when you deploy to App Engine.
You can also include PHP libraries by specifying
php.ini directives and
include statements in your code. However, the preferred
alternative is to use a PHP dependency management tool such as
If you include your application's root directory in the
include_pathdirective of your
If you choose to use Composer to include your pure PHP libraries, you can simply include a single file after your dependencies are installed:
When you use Composer to install your application's dependencies, all of the packages are added within your application's directory under
vendor, which is also where the
autoload.phpfile gets generated.
The App Engine SDK and tools
The SDK for App Engine includes tools for testing your application and uploading application files.
The development server runs your application on your local computer for testing your application.
handles all command-line interaction with your application running on App
Engine. You use
gcloud app deploy to upload your application to App Engine, or
to update individual configuration files. You can also view your app's log data,
so you can analyze your app's performance using your own tools.
PHP interpreter source code
You can download the source code for App Engine's PHP interpreter the appengine-php repository in GitHub.
Concurrency and latency
Your application's latency has the biggest impact on the number of instances needed to serve your traffic. If you process requests quickly, a single instance can handle a lot of requests.