Skip to content

Commit 03d00af

Browse files
committed
Instructions for examples and architecture
1 parent f49bf4f commit 03d00af

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

ARCHITECTURE.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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.

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ and v3.
1717

1818
## Installation
1919

20-
### Composer
20+
### Composer (recommended)
2121

2222
Use [Composer](https://getcomposer.org) to install this library from Packagist:
2323
[`google/recaptcha`](https://packagist.org/packages/google/recaptcha)
@@ -43,7 +43,7 @@ and extract into your project. An autoloader script is provided in
4343
`src/autoload.php` which you can require into your script. For example:
4444

4545
```php
46-
require('/path/to/recaptcha/src/autoload.php');
46+
require_once '/path/to/recaptcha/src/autoload.php';
4747
$recaptcha = new \ReCaptcha\ReCaptcha($secret);
4848
```
4949

@@ -77,9 +77,26 @@ if ($resp->isSuccess()) {
7777
}
7878
```
7979

80-
You can see and run examples in [examples/](examples/) and running at
81-
https://recaptcha-demo.appspot.com/
80+
For more usage details, see [ARCHITECTURE](ARCHITECTURE.md).
81+
82+
### Examples
83+
84+
You can see examples of each reCAPTCHA type in [examples/](examples/). You can
85+
run the examples locally by using the Composer script:
86+
87+
```sh
88+
composer run-script serve-examples
89+
```
90+
91+
This makes use of the in-built PHP dev server to host the examples at
92+
http://localhost:8080/
93+
94+
These are also hosted on Google AppEngine Flexible environment at
95+
https://recaptcha-demo.appspot.com/. This is configured by
96+
[`app.yaml`](./app.yaml) which you can also use to [deploy to your own AppEngine
97+
project](https://cloud.google.com/appengine/docs/flexible/php/download).
8298

8399
## Contributing
84100

85-
We accept contributions via Pull Requests, see [CONTRIBUTING](CONTRIBUTING.md)
101+
No one ever has enough engineers, so we're very happy to accept contributions
102+
via Pull Requests. For details, see [CONTRIBUTING](CONTRIBUTING.md)

0 commit comments

Comments
 (0)