PHP Classes

How to Implement a PHP Google Cloud Response Generator Learning from the Example Hello World HTTP Function: PHP example of Google Cloud function

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-10-10 (Yesterday) RSS 2.0 feedNot enough user ratingsTotal: 10 This week: 10All time: 11,420 This week: 7Up
Version License PHP version Categories
helloworld_http_func 1.0MIT/X Consortium ...5PHP 5, Web services, Hosting
Description 

Author

This package provides a PHP example of the Google Cloud function.

It provides a script that can call the Google Cloud functions PHP framework to install a given cloud function.

The package provides an example function to dispatch HTTP requests received by Google Cloud HTTP serves to output a Hello World message.

Picture of Chun-Sheng, Li
  Performance   Level  
Name: Chun-Sheng, Li <contact>
Classes: 37 packages by
Country: Taiwan Taiwan
Innovation award
Innovation award
Nominee: 17x

Winner: 1x

Example

<?php

use Google\CloudFunctions\FunctionsFramework;
use
Psr\Http\Message\ResponseInterface;
use
Psr\Http\Message\ServerRequestInterface;
use
GuzzleHttp\Psr7\Response;


// Register the function with Functions Framework.
// This enables omitting the `FUNCTIONS_SIGNATURE_TYPE=http` environment
// variable when deploying. The `FUNCTION_TARGET` environment variable should
// match the first parameter.
FunctionsFramework::http('helloHttp', 'helloHttp');

function
helloHttp(ServerRequestInterface $request)
{
   
$headers = ['Access-Control-Allow-Origin' => '*'];
   
$statusCode = 204;

    if (
$request->getMethod() === 'OPTIONS') {
       
// Send response to OPTIONS requests
       
$headers = array_merge($headers, [
           
'Access-Control-Allow-Methods' => 'GET',
           
'Access-Control-Allow-Headers' => 'Content-Type',
           
'Access-Control-Max-Age' => '3600'
       
]);
        return new
Response($statusCode, $headers, '');
    }
   
$name = 'World';
   
$body = $request->getBody()->getContents();
    if (!empty(
$body)) {
       
$json = json_decode($body, true);
        if (
json_last_error() != JSON_ERROR_NONE) {
            throw new
RuntimeException(sprintf(
               
'Could not parse body: %s',
               
json_last_error_msg()
            ));
        }
       
$name = $json['name'] ?? $name;
    }
   
$queryString = $request->getQueryParams();
   
$name = $queryString['name'] ?? $name;
   
$statusCode = 200;

    return new
Response($statusCode, $headers, json_encode(['message' => sprintf('Hello, %s!', htmlspecialchars($name))]), '');
}


Details

helloworld_http

Introduction

This is the example for GCP Cloud function with PHP.

Deployment

  • Clone this repository.
  • Running the `compoer install` command to install required dependencies.
  • Running the following command to deploy this function:
function_name="your function name"
REGION="your GCP region"

gcloud functions deploy ${function_name} \
    --gen2 \
    --runtime=php82 \
    --region="$REGION" \
    --source=. \
    --entry-point=helloHttp \
    --trigger-http \
    --allow-unauthenticated

  • Running the following command to delete the function:
function_name="your function name"
REGION="your GCP region"

gcloud functions delete ${function_name} --region="$REGION"

References

  • https://cloud.google.com/functions/docs/create-deploy-http-php
  • https://cloud.google.com/functions/docs/samples/functions-http-cors

  Files folder image Files (4)  
File Role Description
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:10
This week:10
All time:11,420
This week:7Up