diff --git a/composer.json b/composer.json index 2afaf1d..8fdc1e1 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/src/core/DeCaptchaAbstract.php b/src/core/DeCaptchaAbstract.php index cc52e6b..1744bdb 100644 --- a/src/core/DeCaptchaAbstract.php +++ b/src/core/DeCaptchaAbstract.php @@ -3,6 +3,7 @@ namespace jumper423\decaptcha\core; use Exception; +use GuzzleHttp\Client; /** * Class DeCaptchaAbstract. @@ -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; @@ -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 ); } @@ -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();