|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright 2019 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 | +/** |
| 19 | + * For instructions on how to run the full sample: |
| 20 | + * |
| 21 | + * @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/tasks/README.md |
| 22 | + */ |
| 23 | + |
| 24 | +// Include Google Cloud dependendencies using Composer |
| 25 | +require_once __DIR__ . '/../vendor/autoload.php'; |
| 26 | + |
| 27 | +if ($argc < 5 || $argc > 6) { |
| 28 | + return printf("Usage: php %s PROJECT_ID LOCATION_ID QUEUE_ID URL [PAYLOAD]\n", __FILE__); |
| 29 | +} |
| 30 | +list($_, $projectId, $locationId, $queueId, $url, $payload) = $argv; |
| 31 | + |
| 32 | +# [START cloud_tasks_create_http_task] |
| 33 | +use Google\Cloud\Tasks\V2beta3\CloudTasksClient; |
| 34 | +use Google\Cloud\Tasks\V2beta3\HttpMethod; |
| 35 | +use Google\Cloud\Tasks\V2beta3\HttpRequest; |
| 36 | +use Google\Cloud\Tasks\V2beta3\Task; |
| 37 | + |
| 38 | +/** Uncomment and populate these variables in your code */ |
| 39 | +// $projectId = 'The Google project ID'; |
| 40 | +// $locationId = 'The Location ID'; |
| 41 | +// $queueId = 'The Cloud Tasks Queue ID'; |
| 42 | +// $url = 'The full url path that the task request will be sent to.' |
| 43 | +// $payload = 'The payload your task should carry to the task handler. Optional'; |
| 44 | + |
| 45 | +// Instantiate the client and queue name. |
| 46 | +$client = new CloudTasksClient(); |
| 47 | +$queueName = $client->queueName($projectId, $locationId, $queueId); |
| 48 | + |
| 49 | +// Create an Http Request Object. |
| 50 | +$httpRequest = new HttpRequest(); |
| 51 | +// The full url path that the task request will be sent to. |
| 52 | +$httpRequest->setUrl($url); |
| 53 | +// POST is the default HTTP method, but any HTTP method can be used. |
| 54 | +$httpRequest->setHttpMethod(HttpMethod::POST); |
| 55 | +// Setting a body value is only compatible with HTTP POST and PUT requests. |
| 56 | +if (isset($payload)) { |
| 57 | + $httpRequest->setBody($payload); |
| 58 | +} |
| 59 | + |
| 60 | +// Create a Cloud Task object. |
| 61 | +$task = new Task(); |
| 62 | +$task->setHttpRequest($httpRequest); |
| 63 | + |
| 64 | +// Send request and print the task name. |
| 65 | +$response = $client->createTask($queueName, $task); |
| 66 | +printf('Created task %s' . PHP_EOL, $response->getName()); |
| 67 | + |
| 68 | +# [END cloud_tasks_create_http_task] |
0 commit comments