diff --git a/appengine/php72/tasks/snippets/README.md b/appengine/php72/tasks/snippets/README.md index 05e5b309f9..4264704bf1 100644 --- a/appengine/php72/tasks/snippets/README.md +++ b/appengine/php72/tasks/snippets/README.md @@ -4,7 +4,7 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP. -`src/create_task.php` is a simple function to create app engine queue tasks. +`src/create_task.php` is a simple function to create tasks with App Engine routing. ## Setup: @@ -23,21 +23,34 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP 4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md). Run `php composer.phar install` (if composer is installed locally) or `composer install` (if composer is installed globally). - 5. Create a Queue To create a queue using the Cloud SDK, use the following gcloud command: ```sh gcloud beta tasks queues create-app-engine-queue my-appengine-queue ``` -6. Identify the Location +6. Set environment variables: + + First, your project ID: + + export PROJECT_ID=my-project-id + + Then the queue ID, as specified at queue creation time. Queue IDs already + created can be listed with `gcloud alpha tasks queues list`. + + export QUEUE_ID=my-appengine-queue - Determine the location ID, which can be discovered with - `gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in - the "name" value (for instance, if the name is - "projects/my-project/locations/us-central1/queues/my-pull-queue", then the - location is "us-central1"). + Then, identify the queue location -7. Run `php src/SNIPPET_NAME.php`. The usage will print for each if no arguments are provided: + Determine the location ID, which can be discovered with + `gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in + the "name" value (for instance, if the name is + "projects/my-project/locations/us-central1/queues/my-pull-queue", then the + location is "us-central1"). + + export LOCATION_ID=us-central1 + +## Using App Engine Routing +1. Run `php src/create_task.php`. The usage will print for each if no arguments are provided: ``` $> php src/create_task.php @@ -51,25 +64,3 @@ Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP ## Licensing * See [LICENSE](../../LICENSE) - - - - -## Creating a queue - - -## Running the Samples - -Set the environment variables: - -Set environment variables: - -First, your project ID: - - export PROJECT_ID=my-project-id - -Then the queue ID, as specified at queue creation time. Queue IDs already -created can be listed with `gcloud alpha tasks queues list`. - - export QUEUE_ID=my-appengine-queue - diff --git a/appengine/php72/tasks/snippets/composer.json b/appengine/php72/tasks/snippets/composer.json index 93dbfed676..edfe25e66d 100644 --- a/appengine/php72/tasks/snippets/composer.json +++ b/appengine/php72/tasks/snippets/composer.json @@ -1,6 +1,6 @@ { "require": { - "google/cloud-tasks": "^0.8.0" + "google/cloud-tasks": "^0.9.0" }, "require-dev": { "phpunit/phpunit": "^5", diff --git a/tasks/README.md b/tasks/README.md new file mode 100644 index 0000000000..29964372be --- /dev/null +++ b/tasks/README.md @@ -0,0 +1,58 @@ +# Google Cloud Tasks Samples + +## Description + +Al code in the snippets directory demonstrate how to invoke Cloud Tasks from PHP. + +`src/create_http_task.php` is a simple function to create tasks with an HTTP target. + +## Setup: + +1. **Enable APIs** - [Enable the Cloud Tasks API](https://console.cloud.google.com/flows/enableapi?apiid=cloudtasks) + and create a new project or select an existing project. +2. **Download The Credentials** - Click "Go to credentials" after enabling the APIs. Click "New Credentials" + and select "Service Account Key". Create a new service account, use the JSON key type, and + select "Create". Once downloaded, set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` + to the path of the JSON key that was downloaded. +3. **Clone the repo** and cd into this directory + + ```sh + $ git clone https://github.com/GoogleCloudPlatform/php-docs-samples + $ cd php-docs-samples/tasks + ``` +4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md). + Run `php composer.phar install` (if composer is installed locally) or `composer install` + (if composer is installed globally). +5. Create a Queue + To create a queue using the Cloud SDK, use the following gcloud command: + ```sh + gcloud beta tasks queues create-app-engine-queue my-appengine-queue + ``` + +## Using an HTTP Target +1. Run `php src/create_http_task.php`. The usage will print for each if no arguments are provided: + + ``` + $> php src/create_http_task.php + Usage: php src/create_http_task.php PROJECT_ID LOCATION_ID QUEUE_ID URL [PAYLOAD] + ``` + + where: + * `PROJECT_ID` is your Google Cloud Project id. + * `QUEUE_ID` is your queue id (ie my-appengine-queue). + Queue IDs already created can be listed with `gcloud alpha tasks queues list`. + * `LOCATION_ID` is the location of your queue. + Determine the location ID, which can be discovered with + `gcloud beta tasks queues describe $QUEUE_ID`, with the location embedded in + the "name" value (for instance, if the name is + "projects/my-project/locations/us-central1/queues/my-pull-queue", then the + location is "us-central1"). + * `URL` is the full URL to your target endpoint. + +## Contributing changes + +* See [CONTRIBUTING.md](../../CONTRIBUTING.md) + +## Licensing + +* See [LICENSE](../../LICENSE) diff --git a/tasks/composer.json b/tasks/composer.json new file mode 100644 index 0000000000..edfe25e66d --- /dev/null +++ b/tasks/composer.json @@ -0,0 +1,9 @@ +{ + "require": { + "google/cloud-tasks": "^0.9.0" + }, + "require-dev": { + "phpunit/phpunit": "^5", + "google/cloud-tools": "^0.8.5" + } +} diff --git a/tasks/phpunit.xml.dist b/tasks/phpunit.xml.dist new file mode 100644 index 0000000000..2f8d312733 --- /dev/null +++ b/tasks/phpunit.xml.dist @@ -0,0 +1,31 @@ + + + + + + test + + + + + + + + ./src + + + diff --git a/tasks/src/create_http_task.php b/tasks/src/create_http_task.php new file mode 100644 index 0000000000..febe66df48 --- /dev/null +++ b/tasks/src/create_http_task.php @@ -0,0 +1,68 @@ + 6) { + return printf("Usage: php %s PROJECT_ID LOCATION_ID QUEUE_ID URL [PAYLOAD]\n", __FILE__); +} +list($_, $projectId, $locationId, $queueId, $url, $payload) = $argv; + +# [START cloud_tasks_create_http_task] +use Google\Cloud\Tasks\V2beta3\CloudTasksClient; +use Google\Cloud\Tasks\V2beta3\HttpMethod; +use Google\Cloud\Tasks\V2beta3\HttpRequest; +use Google\Cloud\Tasks\V2beta3\Task; + +/** Uncomment and populate these variables in your code */ +// $projectId = 'The Google project ID'; +// $locationId = 'The Location ID'; +// $queueId = 'The Cloud Tasks Queue ID'; +// $url = 'The full url path that the task request will be sent to.' +// $payload = 'The payload your task should carry to the task handler. Optional'; + +// Instantiate the client and queue name. +$client = new CloudTasksClient(); +$queueName = $client->queueName($projectId, $locationId, $queueId); + +// Create an Http Request Object. +$httpRequest = new HttpRequest(); +// The full url path that the task request will be sent to. +$httpRequest->setUrl($url); +// POST is the default HTTP method, but any HTTP method can be used. +$httpRequest->setHttpMethod(HttpMethod::POST); +// Setting a body value is only compatible with HTTP POST and PUT requests. +if (isset($payload)) { + $httpRequest->setBody($payload); +} + +// Create a Cloud Task object. +$task = new Task(); +$task->setHttpRequest($httpRequest); + +// Send request and print the task name. +$response = $client->createTask($queueName, $task); +printf('Created task %s' . PHP_EOL, $response->getName()); + +# [END cloud_tasks_create_http_task] diff --git a/tasks/test/tasksTest.php b/tasks/test/tasksTest.php new file mode 100644 index 0000000000..fa58c62551 --- /dev/null +++ b/tasks/test/tasksTest.php @@ -0,0 +1,59 @@ +requireEnv('CLOUD_TASKS_APPENGINE_QUEUE'); + $location = $this->requireEnv('CLOUD_TASKS_LOCATION'); + + $output = $this->runSnippet('create_http_task', [ + $location, + $queue, + 'http://example.com/', + 'Task Details', + ]); + $taskNamePrefix = sprintf('projects/%s/locations/%s/queues/%s/tasks/', + self::$projectId, + $location, + $queue + ); + + $expectedOutput = sprintf('Created task %s', $taskNamePrefix); + $this->assertContains($expectedOutput, $output); + } + + private function runSnippet($sampleName, $params = []) + { + $argv = array_merge([0, self::$projectId], array_values($params)); + $argc = count($argv); + ob_start(); + require __DIR__ . "/../src/$sampleName.php"; + return ob_get_clean(); + } +}