How to use Google Cloud Functions using PHP as backend for Android app or Website?

Here you can see step-by-step guide to use Google Cloud Functions using PHP as backend for Android app or Website using “gcloud” tool.

First login into your Google Cloud console account, if you not created project yet then first you need to create a project.

Here we are going to use “composer” to manage dependencies for Google Cloud Functions using PHP Language. So download and install “composer” from below link

https://getcomposer.org/

Now create folder on your local system and add file composer.json and then add below code in it

{
 "require": {
 "google/cloud-functions-framework": "^0.7.2",
 },
 "scripts": {
 "start": [
 "Composer\Config::disableProcessTimeout",
 "FUNCTION_TARGET=api_install php -S localhost:${PORT:-8080} vendor/bin/router.php"
 ]
 }
 }

Here we have given FUNCTION_TARGET=api_install(function name) which is a entry point for our Google Cloud Functions.

Now in order to download / update dependencies open project folder in command line then enter command “composer update” which will download required dependencies and add vendor folder in your project folder. For more information visit below link.

https://cloud.google.com/functions/docs/writing/specifying-dependencies-php

Next create folder name api-function add file index.php in folder which will contain entry point function “api_install”. Add below code in your index.php

 use Psr\Http\Message\ServerRequestInterface;
 function api_install( ServerRequestInterface $request ) {
 $a = array( 'name' => 'Lorem Ipsum' );
 return json_encode( $a );
 }

Here, we have added function api_install which contain array and returns json_encoded array. You can write any code here read data from file, database or display html page just return string or json_encoded data from function.

Now for testing we are going to run function on our local system.

First go to folder from command line then add below command to run Google Cloud Functions

 export FUNCTION_TARGET=api_install
 php -S localhost:8080 vendor/bin/router.php

Now visit or call api http://localhost:8080 from broswer or android

Next we are going to deploy / redeploy our function in Google Cloud, for that we need to download Google Cloud SDK.

So on your local system install or download Google Cloud SDK. Using https://cloud.google.com/sdk/docs/install link you can check information to install Google Cloud SDK platform wise.

Here we are going to use zip file which can be downloaded from url https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-340.0.0-linux-x86_64.tar.gz.

After downloading extract file on your system and then go to bin folder from command line and enter below command

./gcloud functions deploy api-function --region asia-south1 --entry-point api_install --runtime php74 --source /var/www/html/api-function --trigger-http --allow-unauthenticated

api-function is function name in Google Cloud Functions

api_install is function from our index.php file

/var/www/html/api-function is project path from our local system

–trigger-http it means we are going to add function which accessible by url

–allow-unauthenticated when we visit function through url we don’t need to authenticiate it will be accessible by public

For more information visit below link

https://cloud.google.com/functions/docs/writing/http

Now your function is accessible by

https://asia-south1-project-123456.cloudfunctions.net/api-function

project-123456 – it is a project id which is assigned when we create project in Google Cloud

api-function is function name which we enter when deloying function