PHP Cloud Functions on Google Cloud Platform with “Functions Framework for PHP”

Google Cloud Platform has launched official support for PHP Cloud Functions using Functions Framework for PHP.

With it, an HTTP Cloud Function becomes as simple as this:

use Psr\Http\Message\ServerRequestInterface;

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

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

Functions that respond to Cloud Events can work with a \Google\CloudFunctions\CloudEvent instance:

use Google\CloudFunctions\CloudEvent;

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

    // …
}

Installation per Composer

composer require google/cloud-functions-framework

Functions Framework for PHP →
Introducing PHP on Cloud Functions →

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.