|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Copyright 2021 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/recaptcha/README.md |
| 22 | + */ |
| 23 | + |
| 24 | +namespace Google\Cloud\Samples\Recaptcha; |
| 25 | + |
| 26 | +// [START recaptcha_enterprise_create_site_key] |
| 27 | +use Google\Cloud\RecaptchaEnterprise\V1\RecaptchaEnterpriseServiceClient; |
| 28 | +use Google\Cloud\RecaptchaEnterprise\V1\Key; |
| 29 | +use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings; |
| 30 | +use Google\Cloud\RecaptchaEnterprise\V1\WebKeySettings\IntegrationType; |
| 31 | +use Google\ApiCore\ApiException; |
| 32 | + |
| 33 | +/** |
| 34 | + * Create a site key for reCAPTCHA |
| 35 | + * |
| 36 | + * @param string $projectId Your Google Cloud project ID |
| 37 | + * @param string $keyName The name of the key you wish to create |
| 38 | + */ |
| 39 | +function create_key(string $projectId, string $keyName): void |
| 40 | +{ |
| 41 | + $client = new RecaptchaEnterpriseServiceClient(); |
| 42 | + $formattedProject = $client->projectName($projectId); |
| 43 | + |
| 44 | + // Create the settings for the key. |
| 45 | + // In order to create other keys we'll use AndroidKeySettings or IOSKeySettings |
| 46 | + $settings = new WebKeySettings(); |
| 47 | + |
| 48 | + // Allow the key to work for all domains(Not recommended) |
| 49 | + $settings->setAllowAllDomains(true); |
| 50 | + // ...or explicitly set the allowed domains for the key as an array of strings |
| 51 | + // $settings->setAllowedDomains(['']); |
| 52 | + |
| 53 | + // Specify the type of the key |
| 54 | + // - score based key -> IntegrationType::SCORE |
| 55 | + // - checkbox based key -> IntegrationType::CHECKBOX |
| 56 | + // Read https://cloud.google.com/recaptcha-enterprise/docs/choose-key-type |
| 57 | + $settings->setIntegrationType(IntegrationType::CHECKBOX); |
| 58 | + |
| 59 | + $key = new Key(); |
| 60 | + $key->setDisplayName($keyName); |
| 61 | + $key->setWebSettings($settings); |
| 62 | + |
| 63 | + try { |
| 64 | + $createdKey = $client->createKey($formattedProject, $key); |
| 65 | + printf('The key: %s is created.' . PHP_EOL, $createdKey->getName()); |
| 66 | + } catch (ApiException $e) { |
| 67 | + print('createKey() call failed with the following error: '); |
| 68 | + print($e); |
| 69 | + } |
| 70 | +} |
| 71 | +// [END recaptcha_enterprise_create_site_key] |
| 72 | + |
| 73 | +// The following 2 lines are only needed to run the samples |
| 74 | +require_once __DIR__ . '/../../testing/sample_helpers.php'; |
| 75 | +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); |
0 commit comments