PHP code example of christianjbrown / cloud-run-function-lib

1. Go to this page and download the library: Download christianjbrown/cloud-run-function-lib library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

christianjbrown / cloud-run-function-lib example snippets


use ChristianBrown\CloudRunFunction\DataProviderInterface;
use Psr\Http\Message\ServerRequestInterface;

final class MyDataProvider implements DataProviderInterface
{
    /**
     * @return mixed[]
     */
    public function getData(ServerRequestInterface $request): array
    {
        // Your business logic. Throw a UserFriendlyExceptionInterface to return
        // a specific message to the client; any other Throwable is hidden unless DEBUG is on.
        return ['hello' => 'world'];
    }
}

use ChristianBrown\CloudRunFunction\CloudRunFunctionFactory;

$cloudFunction = (new CloudRunFunctionFactory())->createFromEnvironment(new MyDataProvider(), $_ENV);

$response = $cloudFunction->run($request); // ChristianBrown\CloudRunFunction\ResponseInterface (PSR-7)

$factory = new CloudRunFunctionFactory();
$config = $factory->createConfigTransformer()->transform($_ENV); // FunctionConfigInterface
$cloudFunction = $factory->create(new MyDataProvider(), $config);

// 1.x
$config = (new FunctionConfigTransformer())->transform($_ENV);
$cloudFunction = new CloudRunFunction($dataProvider, $config);

// 2.0
$cloudFunction = (new CloudRunFunctionFactory())->createFromEnvironment($dataProvider, $_ENV);

// 2.0, when you hold the config yourself
$factory = new CloudRunFunctionFactory();
$config = $factory->createConfigTransformer()->transform($_ENV);
$cloudFunction = $factory->create($dataProvider, $config);

// 1.x
$config = (new FunctionConfig('rev'))->setDebug(true)->setUseCacheTtl(60);

// 2.0: with-ers return a new instance, assign the result
$config = (new FunctionConfig('rev'))->withDebug(true)->withUseCacheTtl(60);

// 1.x
new JsonSuccessResponse($config, $data, 200, $origin);
new JsonErrorResponse($config, 'message', 500, $origin);

// 2.0
$responses = new JsonResponseFactory(new ResponseBodyBuilder(), new CorsHeaderBuilder(new AllowOriginResolver()), new CacheHeaderBuilder(), new NativeClock());
$responses->success($config, $data, 200, $origin);
$responses->error($config, 'message', 500, $origin);
json
{
    "error": "Not authorized",
    "success": false,
    "timestamp_iso8601": "2026-07-15T12:00:00+00:00",
    "timestamp_unix": 1784030400,
    "version": "my-service-00001-abc"
}