Skip to content

Commit

Permalink
Removed dependency on cURL
Browse files Browse the repository at this point in the history
  • Loading branch information
dmromanov committed Jan 18, 2017
1 parent 8934e5d commit 526509d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 45 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"minimum-stability": "stable",
"require": {
"php": ">=5.6.0",
"ext-curl": "*"
"guzzlehttp/guzzle": "^6.2"
},
"require-dev": {
"phpunit/phpunit": "^4.8.30"
Expand Down
76 changes: 32 additions & 44 deletions src/core/DeCaptchaAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace jumper423\decaptcha\core;

use Exception;
use GuzzleHttp\Client;

/**
* Class DeCaptchaAbstract.
Expand All @@ -17,8 +18,8 @@ abstract class DeCaptchaAbstract implements DeCaptchaInterface
const ACTION_METHOD = 3;
const ACTION_JSON = 4;

const ACTION_METHOD_POST = 1;
const ACTION_METHOD_GET = 2;
const ACTION_METHOD_POST = 'post';
const ACTION_METHOD_GET = 'get';

const DECODE_FORMAT = 1;
const DECODE_ACTION = 2;
Expand Down Expand Up @@ -316,10 +317,10 @@ protected function getParams($action, $field = null)
*/
protected function getResponse($action)
{
return $this->curlResponse(
return $this->sendRequest(
$this->actions[$action][static::ACTION_METHOD],
$this->getActionUrl($action),
$this->getParams($action),
array_key_exists(static::ACTION_METHOD, $this->actions[$action]) && $this->actions[$action][static::ACTION_METHOD] === static::ACTION_METHOD_POST,
array_key_exists(static::ACTION_JSON, $this->actions[$action]) && $this->actions[$action][static::ACTION_JSON] === true
);
}
Expand All @@ -345,53 +346,40 @@ protected function executionDelayed($delay = 0, $callback = null)
}

/**
* @param string $url
* @param array $data
* @param bool $isPost
* @param bool $isJson
* @param string $method Request method
* @param string $url Request URL
* @param array|string $data Request payload
* @param bool $isJson Whether request payload should be serialized as JSON
*
* @throws DeCaptchaErrors
* @throws \InvalidArgumentException Invalid arguments combination
*
* @return string
*/
protected function curlResponse($url, $data, $isPost = true, $isJson = false)
protected function sendRequest($method, $url, $data, $isJson = false)
{
$curl = curl_init();
if ($isJson) {
$data = json_encode($data);
} elseif (!$isPost) {
$uri = [];
foreach ($data as $key => $value) {
$uri[] = "$key=$value";
}
$url .= '?'.implode('&', $uri);
}
curl_setopt($curl, CURLOPT_URL, $url);
if (!$isJson && version_compare(PHP_VERSION, '5.5.0') >= 0 && version_compare(PHP_VERSION, '7.0') < 0 && defined('CURLOPT_SAFE_UPLOAD')) {
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, $isPost);
if ($isPost) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
if ($isJson) {
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Content-Type: application/json; charset=utf-8',
'Accept: application/json',
'Content-Length: '.strlen($data),
]);
$client = new Client([
'timeout' => 30,
'connect_timeout' => 30,
'headers' => [
'Accept-Encoding' => 'gzip,deflate',
'User-Agent' => 'DeCaptcha'
],
]);

if ($method === static::ACTION_METHOD_GET && $isJson) {
throw new \InvalidArgumentException('JSON encoding with GET requests is not supported.');
}
$result = curl_exec($curl);
if (curl_errno($curl)) {
throw new DeCaptchaErrors(DeCaptchaErrors::ERROR_CURL, curl_error($curl), $this->errorLang);
}
curl_close($curl);

return $result;
$options = [];

$encType = $isJson ? 'json' : 'query';
$options[$encType] = $data;

$response = $client->request($method, $url, $options);

$body = $response->getBody();

return (string) $body;
}

abstract public function getCode();
Expand Down

0 comments on commit 526509d

Please sign in to comment.