|
| 1 | +
|
| 2 | +/** |
| 3 | + * Copyright 2020 Google LLC. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +// Include Google Cloud dependendencies using Composer |
| 19 | +require_once __DIR__ . '/../vendor/autoload.php'; |
| 20 | + |
| 21 | +if ($argc < 6 || $argc > 7) { |
| 22 | + return printf("Usage: php %s PROJECT_ID LOCATION_ID QUEUE_ID URL SERVICE_ACCOUNT_EMAIL [PAYLOAD]\n", __FILE__); |
| 23 | +} |
| 24 | +list($_, $projectId, $locationId, $queueId, $url, $serviceAccountEmail) = $argv; |
| 25 | +$payload = isset($payload[6]) ? $payload[6] : null; |
| 26 | + |
| 27 | +# [START cloud_tasks_create_http_task_with_token] |
| 28 | +use Google\Cloud\Tasks\V2\CloudTasksClient; |
| 29 | +use Google\Cloud\Tasks\V2\HttpMethod; |
| 30 | +use Google\Cloud\Tasks\V2\HttpRequest; |
| 31 | +use Google\Cloud\Tasks\V2\Task; |
| 32 | +use Google\Cloud\Tasks\V2\OidcToken; |
| 33 | + |
| 34 | +/** Uncomment and populate these variables in your code */ |
| 35 | +// $projectId = 'The Google project ID'; |
| 36 | +// $locationId = 'The Location ID'; |
| 37 | +// $queueId = 'The Cloud Tasks Queue ID'; |
| 38 | +// $url = 'The full url path that the task request will be sent to.'; |
| 39 | +// $serviceAccountEmail = 'The Cloud IAM service account to be used at the construction of the token'; |
| 40 | +// $payload = 'The payload your task should carry to the task handler. Optional'; |
| 41 | + |
| 42 | +// Instantiate the client and queue name. |
| 43 | +$client = new CloudTasksClient(); |
| 44 | +$queueName = $client->queueName($projectId, $locationId, $queueId); |
| 45 | + |
| 46 | +// Add your service account email to construct the OIDC token |
| 47 | +// in order to add an authentication header to the request. |
| 48 | +$oidcToken = new OidcToken(); |
| 49 | +$oidcToken->setServiceAccountEmail($serviceAccountEmail); |
| 50 | + |
| 51 | +// Create an Http Request Object. |
| 52 | +$httpRequest = new HttpRequest(); |
| 53 | +// The full url path that the task request will be sent to. |
| 54 | +$httpRequest->setUrl($url); |
| 55 | +// POST is the default HTTP method, but any HTTP method can be used. |
| 56 | +$httpRequest->setHttpMethod(HttpMethod::POST); |
| 57 | +//The oidcToken used to assert identity. |
| 58 | +$httpRequest->setOidcToken($oidcToken); |
| 59 | +// Setting a body value is only compatible with HTTP POST and PUT requests. |
| 60 | +if (isset($payload)) { |
| 61 | + $httpRequest->setBody($payload); |
| 62 | +} |
| 63 | + |
| 64 | +// Create a Cloud Task object. |
| 65 | +$task = new Task(); |
| 66 | +$task->setHttpRequest($httpRequest); |
| 67 | + |
| 68 | +// Send request and print the task name. |
| 69 | +$response = $client->createTask($queueName, $task); |
| 70 | +printf('Created task %s' . PHP_EOL, $response->getName()); |
| 71 | + |
| 72 | +# [END cloud_tasks_create_http_task_with_token] |
0 commit comments