Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,10 @@ if ($resp->isSuccess()) {
}
```

By default, this will use the
[`stream_context_create()`](https://secure.php.net/stream_context_create) and
[`file_get_contents()`](https://secure.php.net/file_get_contents) to make a POST
request to the reCAPTCHA service. This is handled by the
[`RequestMethod\Post`](./src/ReCaptcha/RequestMethod/Post.php) class.

## Alternate request methods

You may need to use other methods for making requests in your environment. The
[`ReCaptcha`](./src/ReCaptcha/ReCaptcha.php) class allows an optional
[`RequestMethod`](./src/ReCaptcha/RequestMethod.php) instance to configure this.
For example, if you want to use [cURL](https://secure.php.net/curl) instead you
can do this:

```php
<?php
$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\CurlPost());
```

Alternatively, you can also use a [socket](https://secure.php.net/fsockopen):

```php
<?php
$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\SocketPost());
```
The `ReCaptcha` class automatically chooses a method to communicate with the
reCAPTCHA service based on your server's capabilities. See the
[Alternate request methods](README.md#alternate-request-methods) section in the
README for more details.

## Adding new request methods

Expand Down
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,43 @@ if ($resp->isSuccess()) {
You can find the constants for the libraries error codes in the `ReCaptcha`
class constants, e.g. `ReCaptcha::E_HOSTNAME_MISMATCH`

### Alternate request methods

**Note:** As of version 1.4.2, the default behavior has changed.

By default, the library will attempt to use [cURL](https://secure.php.net/curl) to make the
POST request to the reCAPTCHA service. This is handled by the
[`RequestMethod\CurlPost`](./src/ReCaptcha/RequestMethod/CurlPost.php) class.
If cURL is not available, it will fall back to using
[`stream_context_create()`](https://secure.php.net/stream_context_create) and
[`file_get_contents()`](https://secure.php.net/file_get_contents) via the
[`RequestMethod\Post`](./src/ReCaptcha/RequestMethod/Post.php) class.

To keep the previous behavior of always using `file_get_contents()` regardless of cURL's availability, you can explicitly configure it:

```php
<?php
$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\Post());
```

You may need to use other methods for making requests in your environment. The
[`ReCaptcha`](./src/ReCaptcha/ReCaptcha.php) class allows an optional
[`RequestMethod`](./src/ReCaptcha/RequestMethod.php) instance to configure this.
For example, if you want to force the use of [cURL](https://secure.php.net/curl) you
can do this:

```php
<?php
$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\CurlPost());
```

Alternatively, you can also use a [socket](https://secure.php.net/fsockopen):

```php
<?php
$recaptcha = new \ReCaptcha\ReCaptcha($secret, new \ReCaptcha\RequestMethod\SocketPost());
```

For more details on usage and structure, see [ARCHITECTURE](ARCHITECTURE.md).

### Examples
Expand Down
9 changes: 8 additions & 1 deletion src/ReCaptcha/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,14 @@ public function __construct($secret, ?RequestMethod $requestMethod = null)
}

$this->secret = $secret;
$this->requestMethod = (is_null($requestMethod)) ? new RequestMethod\Post() : $requestMethod;

if (!is_null($requestMethod)) {
$this->requestMethod = $requestMethod;
} elseif (function_exists('curl_version')) {
$this->requestMethod = new RequestMethod\CurlPost();
} else {
$this->requestMethod = new RequestMethod\Post();
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/ReCaptcha/ReCaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ public function testVerifyReturnsErrorOnMissingResponse()
$this->assertEquals([Recaptcha::E_MISSING_INPUT_RESPONSE], $response->getErrorCodes());
}

public function testDefaultRequestMethod()
{
$rc = new ReCaptcha('secret');
$reflection = new \ReflectionClass($rc);
$property = $reflection->getProperty('requestMethod');
$requestMethod = $property->getValue($rc);

if (function_exists('curl_version')) {
$this->assertInstanceOf(RequestMethod\CurlPost::class, $requestMethod);
} else {
$this->assertInstanceOf(RequestMethod\Post::class, $requestMethod);
}
}

public function testVerifyReturnsResponse()
{
$method = $this->getMockRequestMethod('{"success": true}');
Expand Down
Loading