PHP Classes

File: index.php

Recommend this page to a friend!
  Classes of Chun-Sheng, Li   Hello World HTTP Function   index.php   Download  
File: index.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Hello World HTTP Function
PHP example of Google Cloud function
Author: By
Last change:
Date: 2 days ago
Size: 1,593 bytes
 

Contents

Class file image Download
<?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))]), '');
}