Skip to content

Commit

Permalink
Merge pull request #55 from Dann-Forked/guzzle-implementation
Browse files Browse the repository at this point in the history
implement Guzzle usage.
  • Loading branch information
anhskohbo authored Dec 7, 2016
2 parents f00916f + 889a439 commit 5831a8a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- hhvm
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "5.0.*|5.1.*|5.2.*|5.3.*"
"php": ">=5.5.5",
"illuminate/support": "5.0.*|5.1.*|5.2.*|5.3.*",
"guzzlehttp/guzzle": "^6.2"
},
"autoload": {
"psr-4": {
Expand Down
29 changes: 13 additions & 16 deletions src/NoCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Anhskohbo\NoCaptcha;

use Symfony\Component\HttpFoundation\Request;
use GuzzleHttp\Client;

class NoCaptcha
{
Expand All @@ -23,6 +24,11 @@ class NoCaptcha
*/
protected $sitekey;

/**
* @var \GuzzleHttp\Client
*/
protected $http;

/**
* NoCaptcha.
*
Expand All @@ -33,6 +39,9 @@ public function __construct($secret, $sitekey)
{
$this->secret = $secret;
$this->sitekey = $sitekey;
$this->http = new Client([
'timeout' => 2.0,
]);
}

/**
Expand Down Expand Up @@ -107,22 +116,10 @@ public function getJsLink($lang = null)
*/
protected function sendRequestVerify(array $query = [])
{
// This taken from: https://github.com/google/recaptcha/blob/master/src/ReCaptcha/RequestMethod/Post.php
$peer_key = version_compare(PHP_VERSION, '5.6.0', '<') ? 'CN_name' : 'peer_name';

$context = stream_context_create(array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($query, '', '&'),
'verify_peer' => true,
$peer_key => 'www.google.com',
),
));

$response = file_get_contents(static::VERIFY_URL, false, $context);

return json_decode($response, true);
$response = $this->http->request('POST', static::VERIFY_URL, [
'form_params' => $query,
]);
return json_decode($response->getBody(), true);
}

/**
Expand Down

0 comments on commit 5831a8a

Please sign in to comment.