Google-cloud-php: Cloud Functions

Created on 20 Feb 2019  路  2Comments  路  Source: googleapis/google-cloud-php

Hello,

I've searched everywhere on Google and here, but I couldn't seem to find how to execute Cloud Functions on PHP.

Is it available here? Can please someone lead me to the correct documentation/implementation.

Thank you!

Edit:
I would really appreciate if you guys can help me out: https://stackoverflow.com/questions/54797672/call-firebase-cloud-function-that-requires-authentication-using-service-account

question

Most helpful comment

Hi @lambasoft,

Cloud Functions are not included in Google Cloud PHP, but are supported by google/api-client.

Here is an example showing how to execute a cloud function:

$projectId = '[YOUR PROJECT ID]';
$locationId = '[LOCATION ID]';
$functionId = '[YOUR FUNCTION ID]';

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope([
    Google_Service_CloudFunctions::CLOUD_PLATFORM
]);

$functions = new Google_Service_CloudFunctions($client);

$call = new Google_Service_CloudFunctions_CallFunctionRequest();

$name = sprintf(
    'projects/%s/locations/%s/functions/%s',
    $projectId,
    $locationId,
    $functionId
);
$response = $functions->projects_locations_functions->callProjectsLocationsFunctions($name, $call);

print_r($response);

If your function requires data to be provided, you can set that in the Google_Service_CloudFunctions_CallFunctionRequest constructor:

$call = new Google_Service_CloudFunctions_CallFunctionRequest([
    'data' => json_encode([
        'message' => 'Hello World!'
    ])
]);

You will receive an instance of Google_Service_CloudFunctions_CallFunctionResponse as your response.

All 2 comments

it is not available in PHP, only on Node (6, 8), Go and Python

Hi @lambasoft,

Cloud Functions are not included in Google Cloud PHP, but are supported by google/api-client.

Here is an example showing how to execute a cloud function:

$projectId = '[YOUR PROJECT ID]';
$locationId = '[LOCATION ID]';
$functionId = '[YOUR FUNCTION ID]';

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope([
    Google_Service_CloudFunctions::CLOUD_PLATFORM
]);

$functions = new Google_Service_CloudFunctions($client);

$call = new Google_Service_CloudFunctions_CallFunctionRequest();

$name = sprintf(
    'projects/%s/locations/%s/functions/%s',
    $projectId,
    $locationId,
    $functionId
);
$response = $functions->projects_locations_functions->callProjectsLocationsFunctions($name, $call);

print_r($response);

If your function requires data to be provided, you can set that in the Google_Service_CloudFunctions_CallFunctionRequest constructor:

$call = new Google_Service_CloudFunctions_CallFunctionRequest([
    'data' => json_encode([
        'message' => 'Hello World!'
    ])
]);

You will receive an instance of Google_Service_CloudFunctions_CallFunctionResponse as your response.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

castaneai picture castaneai  路  4Comments

mjniuz picture mjniuz  路  6Comments

Google-K picture Google-K  路  6Comments

ammopt picture ammopt  路  6Comments

smalot picture smalot  路  6Comments