Post.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * This is a PHP library that handles calling reCAPTCHA.
  4. *
  5. * @copyright Copyright (c) 2015, Google Inc.
  6. * @link https://www.google.com/recaptcha
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. namespace ReCaptcha\RequestMethod;
  27. use ReCaptcha\ReCaptcha;
  28. use ReCaptcha\RequestMethod;
  29. use ReCaptcha\RequestParameters;
  30. /**
  31. * Sends POST requests to the reCAPTCHA service.
  32. */
  33. class Post implements RequestMethod
  34. {
  35. /**
  36. * URL for reCAPTCHA sitevrerify API
  37. * @var string
  38. */
  39. private $siteVerifyUrl;
  40. /**
  41. * Only needed if you want to override the defaults
  42. *
  43. * @param string $siteVerifyUrl URL for reCAPTCHA sitevrerify API
  44. */
  45. public function __construct($siteVerifyUrl = null)
  46. {
  47. $this->siteVerifyUrl = (is_null($siteVerifyUrl)) ? ReCaptcha::SITE_VERIFY_URL : $siteVerifyUrl;
  48. }
  49. /**
  50. * Submit the POST request with the specified parameters.
  51. *
  52. * @param RequestParameters $params Request parameters
  53. * @return string Body of the reCAPTCHA response
  54. */
  55. public function submit(RequestParameters $params)
  56. {
  57. $options = array(
  58. 'http' => array(
  59. 'header' => "Content-type: application/x-www-form-urlencoded\r\n",
  60. 'method' => 'POST',
  61. 'content' => $params->toQueryString(),
  62. // Force the peer to validate (not needed in 5.6.0+, but still works)
  63. 'verify_peer' => true,
  64. ),
  65. );
  66. $context = stream_context_create($options);
  67. $response = file_get_contents($this->siteVerifyUrl, false, $context);
  68. if ($response !== false) {
  69. return $response;
  70. }
  71. return '{"success": false, "error-codes": ["'.ReCaptcha::E_CONNECTION_FAILED.'"]}';
  72. }
  73. }