Using PHP Libraries

Declaring and managing dependencies

Dependencies for PHP are declared in a standard composer.json file. For example:

{
    "require": {
        "google/cloud": "^0.33"
    }
}

You can use any Linux-compatible PHP package in the App Engine flexible environment. The runtime looks for a composer.json file in your application's source directory and uses composer to install any dependencies before starting your application.

By default, the PHP runtime uses PHP 7.1, but you should explicitly declare your PHP version in the composer.json file to prevent your application from being automatically upgraded when a new version of PHP becomes available. The PHP versions 5.6.* and 7.0.* are also supported.

{
    "require": {
        "php": "7.1.*"
    }
}

Installing and running locally

Use Composer to install your dependencies locally.

composer install

To pin your dependencies to their current version, commit the composer.lock file to your application.

You can test your application using your web server of choice. App Engine flexible environment uses NGINX in production. To quickly run your application, you can use PHP's built-in web server.

Installing a web framework

By default, NGINX is configured to serve all requests through index.php. A framework is not required, but is encouraged. You can use any web framework with App Engine flexible environments, including the following:

To use a particular web framework, just add it to composer.json:

{
    "require": {
        "symfony/symfony": " ^3.0"
    }
}

Installing the Google Cloud Client library

The Google Cloud Client Library for PHP is a client library for accessing Google Cloud Platform services that significantly reduces the boilerplate code you have to write. The library provides high-level API abstractions so they're easier to understand. It embraces idioms of PHP, works well with the standard library, and integrates better with your codebase. All this means you spend more time creating code that matters to you.

To install the library locally:

composer require google/cloud

The client library can automatically handle authentication for you locally as well by using the Google Cloud SDK.

gcloud auth login

Using private repositories

To use libraries in private repositories, you must complete the following tasks:

  • Configure the repository.
  • Give composer the secret to access the private repository.

The following example illustrates how to access a private repository in GitHub.

First, configure the repository in composer.json using vcs for the type:

"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/username/private_package"
    }
]

Next, create a file named auth.json in your project root directory:

{
    "github-oauth": {
        "github.com": "<your-github-auth-token>"
    }
}

You can obtain the GitHub auth token from GitHub's administrative UI.

Here is another example that illustrates how to access a private repository for Bitbucket.

First, configure the repository in composer.json using vcs for the type:

"repositories": [
    {
        "type": "vcs",
        "url":  "https://bitbucket.org/username/private_git"
    }
]

Next, create a file named auth.json in your project root directory:

{
    "bitbucket-oauth": {
        "bitbucket.org": {
            "consumer-key": "<your-oauth-consumer-key>",
            "consumer-secret": "<your-oauth-consumer-secret>"
        }
    }
}

Send feedback about...

App Engine flexible environment for PHP docs