diff --git a/examples/index.php b/examples/index.php
index fa2bc2e..429d62c 100644
--- a/examples/index.php
+++ b/examples/index.php
@@ -71,6 +71,9 @@
General
diff --git a/examples/recaptcha-request-curl.php b/examples/recaptcha-request-curl.php
new file mode 100644
index 0000000..5a6ae36
--- /dev/null
+++ b/examples/recaptcha-request-curl.php
@@ -0,0 +1,151 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+reCAPTCHA demo - Explicit CurlPost
+
+
+ reCAPTCHA demo Explicit CurlPost
+ ↩️ Home
+
+
+
+ Add your keys
+ If you do not have keys already then visit https://www.google.com/recaptcha/admin to generate them. Edit this file and set the respective keys in the config.php file or directly to $siteKey and $secret . Reload the page after this.
+
+ POST data
+
+ setExpectedHostname($serverName)
+ ->verify($responseKey, $remoteAddr)
+ ;
+ if ($resp->isSuccess()) {
+ // If the response is a success, that's it!
+ ?>
+ Success!
+
+ That's it. Everything is working. Go integrate this into your real project.
+ ⤴️ Try again
+
+ Something went wrong
+
+ Check the error code reference at https://developers.google.com/recaptcha/docs/verify#error-code-reference .
+
Note: Error code missing-input-response may mean the user just didn't complete the reCAPTCHA.
+ ⤴️ Try again
+
+ Complete the reCAPTCHA then submit the form.
+
+
+
+
+
+
+
+
diff --git a/examples/recaptcha-request-post.php b/examples/recaptcha-request-post.php
new file mode 100644
index 0000000..0fb2c5e
--- /dev/null
+++ b/examples/recaptcha-request-post.php
@@ -0,0 +1,151 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+reCAPTCHA demo - Explicit Post
+
+
+ reCAPTCHA demo Explicit Post
+ ↩️ Home
+
+
+
+ Add your keys
+ If you do not have keys already then visit https://www.google.com/recaptcha/admin to generate them. Edit this file and set the respective keys in the config.php file or directly to $siteKey and $secret . Reload the page after this.
+
+ POST data
+
+ setExpectedHostname($serverName)
+ ->verify($responseKey, $remoteAddr)
+ ;
+ if ($resp->isSuccess()) {
+ // If the response is a success, that's it!
+ ?>
+ Success!
+
+ That's it. Everything is working. Go integrate this into your real project.
+ ⤴️ Try again
+
+ Something went wrong
+
+ Check the error code reference at https://developers.google.com/recaptcha/docs/verify#error-code-reference .
+
Note: Error code missing-input-response may mean the user just didn't complete the reCAPTCHA.
+ ⤴️ Try again
+
+ Complete the reCAPTCHA then submit the form.
+
+
+
+
+
+
+
+
diff --git a/examples/recaptcha-request-socket.php b/examples/recaptcha-request-socket.php
new file mode 100644
index 0000000..6be0219
--- /dev/null
+++ b/examples/recaptcha-request-socket.php
@@ -0,0 +1,151 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+reCAPTCHA demo - Explicit SocketPost
+
+
+ reCAPTCHA demo Explicit SocketPost
+ ↩️ Home
+
+
+
+ Add your keys
+ If you do not have keys already then visit https://www.google.com/recaptcha/admin to generate them. Edit this file and set the respective keys in the config.php file or directly to $siteKey and $secret . Reload the page after this.
+
+ POST data
+
+ setExpectedHostname($serverName)
+ ->verify($responseKey, $remoteAddr)
+ ;
+ if ($resp->isSuccess()) {
+ // If the response is a success, that's it!
+ ?>
+ Success!
+
+ That's it. Everything is working. Go integrate this into your real project.
+ ⤴️ Try again
+
+ Something went wrong
+
+ Check the error code reference at https://developers.google.com/recaptcha/docs/verify#error-code-reference .
+
Note: Error code missing-input-response may mean the user just didn't complete the reCAPTCHA.
+ ⤴️ Try again
+
+ Complete the reCAPTCHA then submit the form.
+
+
+
+
+
+
+
+
diff --git a/src/ReCaptcha/RequestMethod/SocketPost.php b/src/ReCaptcha/RequestMethod/SocketPost.php
index e0a6a81..40ecd99 100644
--- a/src/ReCaptcha/RequestMethod/SocketPost.php
+++ b/src/ReCaptcha/RequestMethod/SocketPost.php
@@ -97,17 +97,14 @@ public function submit(RequestParameters $params): string
$request .= $content."\r\n\r\n";
fwrite($handle, $request);
- $response = '';
-
- while (!feof($handle)) {
- $line = fgets($handle, 4096);
- if (is_string($line)) {
- $response .= $line;
- }
- }
+ $response = stream_get_contents($handle);
fclose($handle);
+ if (!is_string($response)) {
+ $response = '';
+ }
+
if (0 !== strpos($response, 'HTTP/1.0 200 OK')) {
return '{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}';
}
diff --git a/tests/ReCaptcha/RequestMethod/SocketPostTest.php b/tests/ReCaptcha/RequestMethod/SocketPostTest.php
index c7ecb14..8fdb853 100644
--- a/tests/ReCaptcha/RequestMethod/SocketPostTest.php
+++ b/tests/ReCaptcha/RequestMethod/SocketPostTest.php
@@ -84,21 +84,23 @@ function fwrite(\stdClass $handle, string $string, ?int $length = null): int
}
/**
- * Mock fgets in the ReCaptcha\RequestMethod namespace.
+ * Mock stream_get_contents in the ReCaptcha\RequestMethod namespace.
*/
-function fgets(\stdClass $handle, ?int $length = null): false|string
+function stream_get_contents(\stdClass $handle, ?int $length = null, int $offset = -1): false|string
{
- $response = array_shift(SocketPostGlobalState::$fgetsResponses);
+ if (empty(SocketPostGlobalState::$fgetsResponses)) {
+ return false;
+ }
- return null === $response ? false : $response;
-}
+ $result = '';
+ foreach (SocketPostGlobalState::$fgetsResponses as $response) {
+ if (false !== $response) {
+ $result .= $response;
+ }
+ }
+ SocketPostGlobalState::$fgetsResponses = [];
-/**
- * Mock feof in the ReCaptcha\RequestMethod namespace.
- */
-function feof(\stdClass $handle): bool
-{
- return empty(SocketPostGlobalState::$fgetsResponses);
+ return $result;
}
/**
@@ -226,4 +228,14 @@ public function testBadResponseReturnsError(): void
$this->assertEquals('{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}', $response);
}
+
+ public function testStreamGetContentsReturnsFalse(): void
+ {
+ SocketPostGlobalState::$fgetsResponses = [];
+
+ $sp = new SocketPost();
+ $response = $sp->submit(new RequestParameters('secret', 'response'));
+
+ $this->assertEquals('{"success": false, "error-codes": ["'.ReCaptcha::E_BAD_RESPONSE.'"]}', $response);
+ }
}