PHP 7.1 for Google App Engine is generally available

By Brent Shaffer and Takashi Matsuo, Developer Programs Engineer, Google Cloud Platform

Earlier this year, we announced PHP 7.1 Google App Engine in beta. We PHP lovers at Google are proud to announce that PHP 7.1 is now generally available on Google App Engine, our easy-to-use platform for building, deploying, managing and automatically scaling services on Google’s infrastructure. The PHP 7.1 runtime is available for App Engine flexible environment.

Along with achieving general availability for PHP 7.1, the new PHP runtime includes a few exciting new features.

Extension enabler

The list of PHP extensions you can use with the App Engine runtime now includes any PHP extension that is stable (1.0 and above) and does not require a EULA. You can find a complete list on the PHP runtime page.

This is great news, but to make it even better you can now quickly and easily activate any of these extensions in your deployment by requiring them with Composer.

composer require “ext-gd:*” –ignore-platform-reqs

This is equivalent to adding the name of the extension, prefixed with “ext-”, to your application’s composer.json:
{
    “require”: {
        “ext-gd”: “*”
    }
}

You can still install a custom extension or a specific version of a supported extension, but this will require extending the PHP runtime.

Stackdriver Logging and Error Reporting support

Logging to Stackdriver from your PHP application is very simple. The first requirement is to install the Composer package google/cloud version 0.33 or higher.

composer require google/cloud

Now set enable_stackdriver_integration to true in the runtime_config section of app.yaml:

runtime_config:
  enable_stackdriver_integration: true

This configuration tells the PHP runtime to dispatch the batch-processing daemon to handle your logs behind the scenes. This ensures that when your application writes a log, calls to the Stackdriver API happen in another process so as not to add latency to your application.
Now you can create a PsrBatchLogger instance in your code and log to Stackdriver:

use GoogleCloudLoggingLoggingClient;
use PsrLogLogLevel;

$batchLogger = LoggingClient::psrBatchLogger(‘app’); // logName `app`

// You now have a PSR-3 compliant logger!

$batchLogger->log(LogLevel::DEBUG, ‘This is a debug log’);
$batchLogger->debug(‘This is a debug log’); // The same thing

Additionally, the enable_stackdriver_integration flag sets up an auto-prepend file for error reporting. This means uncaught exceptions and fatal errors are formatted and visible in Cloud Console Error Reporting!

throw new Exception(‘Uncaught exceptions are logged to Stackdriver!’);

$obj->thisDoesNotExist(); // Fatal errors are also logged to Stackdriver!

If you’d rather use individual Google Cloud packages, ensure you have both google/cloud-logging and google/cloud-error-reporting installed. When installing locally, use –ignore-platform-reqs or install gRPC according to the installation instructions.

composer require google/cloud-logging google/cloud-error-reporting

    “ext-grpc:*” –ignore-platform-reqs

Stackdriver Logging for Laravel
After you’ve configured the enable_stackdriver_integration, you can enable Stackdriver integration in an app based on the Laravel PHP framework by modifying bootstrap/app.php:

// Add `Use` for logging libs
use GoogleCloudLoggingLoggingClient;
use MonologHandlerPsrHandler;

// … other code

// Just before returning $app
if (isset($_SERVER[‘GAE_SERVICE’])) {
    $app->configureMonologUsing(function ($monolog) {
        $logger = LoggingClient::psrBatchLogger(‘app’);
        $handler = new PsrHandler($logger);
        $monolog->pushHandler($handler);
    });
}

return $app;

For Error Reporting, add an import and call our `exceptionHandler` in the `report` function in `app/Exceptions/Handler.php`:

use GoogleCloudErrorReportingBootstrap;

// .. other code
    public function report(Exception $exception)
    {
        if (isset($_SERVER[‘GAE_SERVICE’])) {
            Bootstrap::exceptionHandler($exception);<
        } else {
            parent::report($exception);
        }
    }

That’s it! The logs are sent to Stackdriver Logging with the logName ‘app’. Unhandled exceptions are sent to the logName ‘app-error’, and will show up in Stackdriver Error Reporting.

gRPC Support
Google’s highly performant RPC protocol gRPC is built right into the PHP runtime. This allows you to quickly get started making calls to Cloud Spanner and other gRPC-specific APIs. You can enable the extension in composer.json and download the client library:

composer require “ext-grpc:*” google/cloud-spanner –ignore-platform-reqs

Now you can make calls to Spanner in your application:

<?php
// index.php

require __DIR__ . ‘/vendor/autoload.php';

$spanner = new GoogleCloudSpannerSpannerClient();
$instance = $spanner->instance(‘YOUR_INSTANCE_ID’);
$database = $instance->database(‘YOUR_DATABASE_ID’);

# Execute a simple SQL statement.
echo $database->execute(‘SELECT “Hello Spanner” as test’);

By replacing YOUR_INSTANCE_ID and YOUR_DATABASE_ID with the proper values, your application now displays the text “Hello Spanner”!

Our commitment to PHP and open source
At Google, we’re committed to open source — and that goes for the new core PHP Docker runtime, google-cloud composer package and Google API client:

https://github.com/GoogleCloudPlatform/php-docker

https://github.com/GoogleCloudPlatform/google-cloud-php

https://github.com/google/google-api-php-client

https://github.com/GoogleCloudPlatform/wordpress-plugins

https://github.com/grpc/grpc-php

https://github.com/census-instrumentation/opencensus-php

We’re thrilled to welcome PHP developers to Google Cloud Platform, and we’re invested in making you as productive as possible. This is just the start — stay tuned to the blog and our GitHub repositories to catch the next wave of PHP support on GCP.

We can’t wait to hear from you. Feel free to reach out to us on Twitter, or request an invite to the Google Cloud Slack community and join the #PHP channel.

Quelle: Google Cloud Platform