Create and deploy an HTTP Cloud Run function with PHP
This guide walks you through the process of writing a Cloud Run function using the PHP runtime, then testing and deploying the HTTP function.
You can create two types of Cloud Run functions:
- An HTTP function, which you invoke from standard HTTP requests.
- An event-driven function, which you use to handle events from your Cloud infrastructure, such as messages on a Pub/Sub topic, or changes in a Cloud Storage bucket.
For more detail, see writing HTTP functions and writing event-driven functions.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, and Cloud Logging APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, and Cloud Logging APIs.
- Install and initialize the gcloud CLI.
- Update and install
gcloud
components with the following command.gcloud components update
-
Prepare your development environment.
Create your function
Create a directory on your local system for the function code:
Linux or Mac OS X
mkdir ~/helloworld_http cd ~/helloworld_http
Windows
mkdir %HOMEPATH%\helloworld_http cd %HOMEPATH%\helloworld_http
Create an
index.php
file in thehelloworld_http
directory with the following contents:This example function takes a name supplied in the HTTP request and returns a greeting, or "Hello World!" when no name is supplied. For more discussion of the structure and required elements in a PHP HTTP function, see Write HTTP functions.
Specify dependencies
PHP uses Composer to manage dependencies. If you're using Cloud Shell, composer is pre-installed. Otherwise, follow Composer's installation instructions.
Specify your function's dependencies:
Add a
composer.json
file with the following contents to thehelloworld_http
directory:
The
FUNCTION_TARGET
line specifies your function's entry point.- Run the following command in the
helloworld_http
directory:
composer require google/cloud-functions-framework
This adds the Functions Framework to your
composer.json
and creates avendor
directory insidehelloworld_http
that contains the dependencies.
Build and test your function locally
To build and test your function locally before deploying it, perform the following steps:
Create a local web server running your
helloHttp
function:export FUNCTION_TARGET=helloHttp composer start
Test your function by visiting
http://localhost:8080
in a browser or by runningcurl localhost:8080
from another window.See Sending requests to local functions for more detail.
This sample function returns a cheery "Hello, World!" message.
Deploy your function
To deploy your function, run the following command in the
helloworld_http
directory:
gcloud functions deploy php-http-function \
--gen2 \
--runtime=php83 \
--region=REGION \
--source=. \
--entry-point=helloHttp \
--trigger-http \
--allow-unauthenticated
Replace REGION with the name of the Google Cloud region where you want to deploy your function
(for example, us-west1
).
The optional --allow-unauthenticated
flag lets you reach your function
without authentication.
Test your deployed function
After the function deploys, note the
uri
property from the output of thegcloud functions deploy
command, or retrieve it with the following command:gcloud functions describe php-http-function \ --region=REGION
Replace REGION with the name of the Google Cloud region where you deployed your function (for example,
us-west1
).Visit this URL in your browser. The function returns a "Hello World!" message.
You can also find this URL in Google Cloud console. Go to the Cloud Run functions Overview page, and click the name of your function to open its Function details page. Open the TRIGGER tab to see your function's URL.
View your function's logs
View the logs with the command-line tool
You can review your function's logs with the Cloud Logging UI or via the Google Cloud CLI.
To view logs for your function with the gcloud CLI, use the
logs read
command:
gcloud functions logs read \
--gen2 \
--limit=10 \
--region=REGION \
php-http-function
Replace REGION with the name of the Google Cloud region where you deployed your function
(for example, us-west1
).
The output resembles the following:
LEVEL: I
NAME: hellohttp
TIME_UTC: 2023-06-02 19:01:36.067
LOG:
LEVEL: I
NAME: hellohttp
TIME_UTC: 2023-06-02 19:01:22.814
LOG: Default STARTUP TCP probe succeeded after 1 attempt for container "hello_http-1" on port 8080.
LEVEL: I
NAME: hellohttp
TIME_UTC: 2023-06-02 19:01:22.777
LOG: [pid1-nginx] Starting nginx (pid 17): /usr/sbin/nginx -c /tmp/nginxconf-953701689/nginx.conf [session:R8F8ZJ5]
LEVEL: I
NAME: hellohttp
TIME_UTC: 2023-06-02 19:01:22.766
LOG: [pid1-nginx] Successfully connected to /tmp/google-config/app.sock after 556.430499ms [session:R8F8ZJ5]
View logs with the logging dashboard
To view the logs for your function with the logging dashboard, open the Cloud Run functions Overview page and click the name of your function from the list, then click the Logs tab.