This sample demonstrates how to use Google Cloud Endpoints using PHP.
For a complete walkthrough showing how to run this sample in different environments, see the Google Cloud Endpoints Quickstarts.
This sample consists of two parts:
- The backend
- The clients
Install all the dependencies:
$ composer install
Run the application:
$ php -S localhost:8080
With the app running locally, you can execute the simple echo client using:
$ php endpoints.php make-request http://localhost:8080 APIKEY
The APIKEY
can be any string as the local endpoint proxy doesn't need authentication.
See the Google Cloud Endpoints Quickstarts.
With the project deployed, you'll need to create an API key to access the API.
- Open the Credentials page of the API Manager in the Cloud Console.
- Click 'Create credentials'.
- Select 'API Key'.
- Choose 'Server Key'
With the API key, you can use the echo client to access the API:
$ php endpoints.php make-request https://YOUR-PROJECT-ID.appspot.com YOUR-API-KEY
The JWT client demonstrates how to use service accounts to authenticate to endpoints. To use the client, you'll need both an API key (as described in the echo client section) and a service account. To create a service account:
- Open the Credentials page of the API Manager in the Cloud Console.
- Click 'Create credentials'.
- Select 'Service account key'.
- In the 'Select service account' dropdown, select 'Create new service account'.
- Choose 'JSON' for the key type.
- Click on your newly created service account credentials and then click the 'Download JSON' button to download a json file with your credentials. You will use this later.
To use the service account for authentication:
-
Update
YOUR-SERVICE-ACCOUNT-EMAIL
with your service account's email address inopenapi.yaml
(if you're using GKE or GCE) oropenapi-appengine.yaml
(if you're using App Engine Flex).google_jwt: # Update this with your service account's email address. x-google-jwks_uri: "https://www.googleapis.com/service_accounts/v1/jwk/YOUR-SERVICE-ACCOUNT-EMAIL"
-
Redeploy your application.
gcloud app deploy
Now you can use the JWT client to make requests to the API:
$ php endpoints.php make-request https://YOUR-PROJECT-ID.appspot.com YOUR-API-KEY /path/to/service-account.json
The ID Token client demonstrates how to use user credentials to authenticate to endpoints. To use the client, you'll need both an API key (as described in the echo client section) and a OAuth2 client ID. To create a client ID:
- Open the Credentials page of the API Manager in the Cloud Console.
- Click 'Create credentials'.
- Select 'OAuth client ID'.
- Choose 'Other' for the application type.
- Click on your newly created client credentials and then click the 'Download JSON' button to download a json file with your credentials. You will use this later.
To use the client ID for authentication:
-
Update
YOUR-CLIENT-ID
in with your client ID inopenapi.yaml
(if you're using GKE or GCE) oropenapi-appengine.yaml
(if you're using App Engine Flex).google_id_token: # Your OAuth2 client's Client ID must be added here. You can add # multiple client IDs to accept tokens from multiple clients. x-google-jwks_uri: "YOUR-CLIENT-ID"
-
Redeploy your application.
gcloud app deploy
Now you can use the client ID to make requests to the API:
$ php endpoints.php make-request https://YOUR-PROJECT-ID.appspot.com YOUR-API-KEY /path/to/client-secrets.json
If you experience any issues, try running gcloud endpoints configs describe
to
debug the service:
gcloud endpoints configs describe YOUR-CONFIG-ID --service=YOUR-PROJECT-ID.appspot.com
By using Endpoints, you get access to several metrics that are displayed graphically in the Cloud Console.
To view the Endpoints graphs:
- Go to the Endpoints section in Cloud Console of the project you deployed your API to.
- Click on your API to view more detailed information about the metrics collected.
The Swagger UI is an open source Swagger project that allows you to explore your API through a UI. Find out more about it on the Swagger site.