|
| 1 | +# Architecture |
| 2 | + |
| 3 | +The general pattern of usage is to instantiate the |
| 4 | +[`ReCaptcha`](./src/ReCaptcha/ReCaptcha.php) client with your secret key and |
| 5 | +then call `verify()` using the response from the JavaScript reCAPTCHA client. |
| 6 | + |
| 7 | +```php |
| 8 | +<?php |
| 9 | +$recaptcha = new \ReCaptcha\ReCaptcha($secret); |
| 10 | +$resp = $recaptcha->verify($gRecaptchaResponse, $remoteIp); |
| 11 | +if ($resp->isSuccess()) { |
| 12 | + // Verified! |
| 13 | +} else { |
| 14 | + $errors = $resp->getErrorCodes(); |
| 15 | +} |
| 16 | +``` |
| 17 | + |
| 18 | +By default, this will use the |
| 19 | +[`stream_context_create()`](https://secure.php.net/stream_context_create) and |
| 20 | +[`file_get_contents()`](https://secure.php.net/file_get_contents) to make a POST |
| 21 | +request to the reCAPTCHA service. This is handled by the |
| 22 | +[`RequestMethod\Post`](./src/ReCaptcha/RequestMethod/Post.php) class. |
| 23 | + |
| 24 | +## Alternate request methods |
| 25 | + |
| 26 | +You may need to use other methods for making requests in your environment. The |
| 27 | +[`ReCaptcha`](./src/ReCaptcha/ReCaptcha.php) class allows an optional |
| 28 | +[`RequestMethod`](./src/ReCaptcha/RequestMethod.php) instance to configure this. |
| 29 | +For example, if you want to use [cURL](https://secure.php.net/curl) instead you |
| 30 | +can do this: |
| 31 | + |
| 32 | +```php |
| 33 | +<?php |
| 34 | +$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\CurlPost()); |
| 35 | +``` |
| 36 | + |
| 37 | +Alternatively, you can also use a [socket](https://secure.php.net/fsockopen): |
| 38 | + |
| 39 | +```php |
| 40 | +<?php |
| 41 | +$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\SocketPost()); |
| 42 | +``` |
| 43 | + |
| 44 | +## Adding new request methods |
| 45 | + |
| 46 | +Create a class that implements the |
| 47 | +[`RequestMethod`](./src/ReCaptcha/RequestMethod.php) interface. The convention |
| 48 | +is to name this class `RequestMethod\`_MethodType_`Post` and create a separate |
| 49 | +`RequestMethod\`_MethodType_ class that wraps just the calls to the network |
| 50 | +calls themselves. This means that the `RequestMethod\`_MethodType_`Post` can be |
| 51 | +unit tested by passing in a mock. Take a look at |
| 52 | +[`RequestMethod\CurlPost`](./src/ReCaptcha/RequestMethod/CurlPost.php) and |
| 53 | +[`RequestMethod\Curl`](./src/ReCaptcha/RequestMethod/Curl.php) with the matching |
| 54 | +[`RequestMethod/CurlPostTest`](./tests/ReCaptcha/RequestMethod/CurlPostTest.php) |
| 55 | +to see this pattern in action. |
0 commit comments