Application Development

Introducing PHP on Cloud Functions

#appdev

Cloud Functions, Google Cloud’s Function as a Service (FaaS) offering, is a lightweight, easy-to-use compute platform for creating single-purpose, stand-alone functions that respond to events, without having to manage a server or runtime environment. Cloud Functions is a great fit for serverless, application, mobile or IoT backends, real-time data processing systems, video, image and sentiment analysis, and even things like chatbots and virtual assistants.

Today we’re bringing support for PHP, a popular general-purpose programming language, to Cloud Functions. With the Functions Framework for PHP, you can write idiomatic PHP functions to build business-critical applications and integration layers. And with Cloud Functions for PHP, now available in Preview, you can deploy functions in a fully managed PHP 7.4 environment, complete with access to resources in a private VPC network. PHP functions scale automatically based on your load. You can write HTTP functions to respond to HTTP events, and CloudEvent functions to process events sourced from external and internal services including Pub/Sub, Cloud Storage and Firestore.

PHP on Cloud Functions.jpg
Click to enlarge

You can develop functions using the Functions Framework for PHP, an open source functions-as-a-service framework for writing portable PHP functions. With the Functions Framework you can develop, test, and run your functions locally and deploy them to Cloud Functions or another PHP hosting environment.

Writing PHP functions

The Functions Framework for PHP supports HTTP functions and CloudEvent functions. A HTTP function is similar to a Webhook, whereas a CloudEvent function responds to Google services, such as Pub/Sub, Cloud Storage and Firestore, using CNCF CloudEvents.

Here's an example of a very simple HTTP function:

  use Psr\Http\Message\ServerRequestInterface;

function helloHttp(ServerRequestInterface $request): string
{
    $queryString = $request->getQueryParams();
    $name = $queryString['name'] ?? $name;

    return sprintf('Hello, %s!', $name);
}

Here's an example of a very simple CloudEvent function working with Pub/Sub:

  use Google\CloudFunctions\CloudEvent;

function helloworldPubsub(CloudEvent $event): void
{
    $name = 'World';
    $cloudEventData = $event->getData();
    if (!empty($cloudEventData['data'])) {
        $name = base64_decode($cloudEventData['data']);
    }

    // write the result to Cloud Logging
    $result = sprintf('Hello, %s!', $name);
    fwrite(fopen('php://stderr', 'wb'), $result);
}

Logging

Cloud functions on PHP supports logging through Cloud Logging, so information and error messages should be logged using Cloud Logging client library or using stderr, which will then be visible in the Logging UI.

Using PHP libraries

The PHP Functions Framework fits comfortably with popular PHP development processes and tools. Include a composer.json file in your deployment, and those packages will be installed and the autoloader will be registered. Include a php.ini file, and your custom configuration will be loaded and extensions enabled. See dynamically loadable extensions for a complete list.

Try Cloud Functions for PHP today

Cloud Functions for PHP is ready for you to try today. Read the Quickstart guide, try one of our many Cloud Functions tutorials, and do it all with a Google Cloud free trial. If you want to dive a little bit deeper into the technical details, you can take a look at the PHP Functions Framework on GitHub and potentially even contribute. We’re looking forward to seeing all the PHP functions you write!

Useful links