Skip to content

Commit

Permalink
Stability bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
greezlu committed Oct 31, 2023
1 parent ef066c4 commit 181c532
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 46 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ More in the [documentation.](https://docs.capsolver.com/guide/captcha/ReCaptchaV

Request:
```php
$solution = $solver->recaptchaV3([
'type' => \Capsolver\Solvers\Token\ReCaptchaV3::TASK,
'websiteURL' => 'https://www.google.com/recaptcha/api2/demo',
'websiteKey' => '6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-',
'pageAction' => 'verify',
'minScore' => 0.6,
'proxy' => 'http:ip:port:user:pass'
]);
$solution = $solver->recaptchaV3(
\Capsolver\Solvers\Token\ReCaptchaV3::TASK,
[
'websiteURL' => 'https://www.google.com/recaptcha/api2/demo',
'websiteKey' => '6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-',
'pageAction' => 'verify',
'minScore' => 0.6,
'proxy' => 'http:ip:port:user:pass'
]
);
```

Response:
Expand All @@ -54,7 +56,7 @@ If case of an error solver throws an instance of `CapsolverException` exception.

```php
try {
$solution = $solver->recaptchaV3([]);
$solution = $solver->recaptchaV3('', []);
} catch (\Capsolver\Exceptions\RequestException $error) {
// Error happened before api request
} catch (\Capsolver\Exceptions\ResponseException $error) {
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "greezlu/capsolver-php",
"type": "library",
"version": "0.9.0",
"version": "0.9.1",
"autoload": {
"psr-4": {
"Capsolver\\": "src/"
Expand All @@ -15,7 +15,8 @@
],
"require": {
"php": ">=7.4",
"ext-curl": "*"
"ext-curl": "*",
"ext-json": "*"
},
"license": "MIT"
}
11 changes: 10 additions & 1 deletion src/Abstracts/RecognitionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
namespace Capsolver\Abstracts;

use Capsolver\Exceptions\CapsolverException;
use Capsolver\Interfaces\SolverInterface;

abstract class RecognitionAbstract extends TaskAbstract
abstract class RecognitionAbstract
extends TaskAbstract
implements SolverInterface
{
/**
* @param array $params
* @return array
*/
abstract public function solve(array $params): array;

/**
* @param array $request
* @return array
Expand Down
40 changes: 34 additions & 6 deletions src/Abstracts/TaskAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@

abstract class TaskAbstract
{
private const HOST = 'https://api.capsolver.com/';
protected const HOST = 'https://api.capsolver.com/';

private string $key;

/**
* @param string $key
*/
public function __construct(
string $key
) {
$this->key = $key;
}

/**
* @param array $request
Expand All @@ -22,7 +33,7 @@ protected function createTask(array $request): array
{
return $this->send(
self::HOST . 'createTask',
$this->encode($request)
$this->encode($this->hydrate($request))
);
}

Expand All @@ -36,21 +47,20 @@ protected function getTaskResult(array $request): array
{
return $this->send(
self::HOST . 'getTaskResult',
$this->encode($request)
$this->encode($this->hydrate($request, false))
);
}

/**
* @param array $request
* @return array
*
* @throws CapsolverException
*/
protected function getBalance(array $request): array
protected function getBalance(): array
{
return $this->send(
self::HOST . 'getBalance',
$this->encode($request)
$this->encode($this->hydrate([], false))
);
}

Expand All @@ -62,6 +72,24 @@ protected function getBalance(array $request): array
*/
abstract protected function process(array $request): array;

/**
* @param array $params
* @param bool $isTask
* @return array
*/
private function hydrate(
array $params,
bool $isTask = true
): array {
$request = $isTask
? ['task' => $params]
: $params;

$request['clientKey'] = $this->key;

return $request;
}

/**
* @param array $data
* @return string
Expand Down
11 changes: 10 additions & 1 deletion src/Abstracts/TokenAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
namespace Capsolver\Abstracts;

use Capsolver\Exceptions\CapsolverException;
use Capsolver\Interfaces\SolverInterface;

abstract class TokenAbstract extends TaskAbstract
abstract class TokenAbstract
extends TaskAbstract
implements SolverInterface
{
/**
* @param array $params
* @return array
*/
abstract public function solve(array $params): array;

/**
* @param array $request
* @return array
Expand Down
25 changes: 9 additions & 16 deletions src/CapsolverClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Capsolver;

use Capsolver\Exceptions\CapsolverException;
use Capsolver\Solvers\Token\ReCaptchaV3;

class CapsolverClient
{
Expand All @@ -20,26 +21,18 @@ public function __construct(
}

/**
* @param string $type
* @param array $params
* @return array
*
* @throws CapsolverException
*/
public function recaptchaV3(array $params): array
{
$solver = new \Capsolver\Solvers\Token\ReCaptchaV3();
return $solver->solve($this->hydrate($params));
}

/**
* @param array $params
* @return array
*/
private function hydrate(array $params): array
{
return [
'clientKey' => $this->key,
'task' => $params
];
public function recaptchaV3(
string $type,
array $params
): array {
$params['type'] = $type;
$solver = new ReCaptchaV3($this->key);
return $solver->solve($params);
}
}
4 changes: 2 additions & 2 deletions src/Interfaces/SolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
interface SolverInterface
{
/**
* @param array $request
* @param array $params
* @return array
*
* @throws CapsolverException
*/
public function solve(array $request): array;
public function solve(array $params): array;
}
17 changes: 8 additions & 9 deletions src/Solvers/Token/ReCaptchaV3.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

use Capsolver\Exceptions\CapsolverException;
use Capsolver\Exceptions\RequestException;
use Capsolver\Interfaces\SolverInterface;
use Capsolver\Abstracts\TokenAbstract;

class ReCaptchaV3 extends TokenAbstract implements SolverInterface
class ReCaptchaV3 extends TokenAbstract
{
public const TASK = 'ReCaptchaV3Task';
public const ENTERPRISE_TASK = 'ReCaptchaV3EnterpriseTask';
Expand All @@ -18,25 +17,25 @@ class ReCaptchaV3 extends TokenAbstract implements SolverInterface
public const M1_TASK_PROXYLESS = 'ReCaptchaV3M1TaskProxyLess';

/**
* @param array $request
* @param array $params
* @return array
*
* @throws CapsolverException
*/
public function solve(
array $request
array $params
): array {
$this->validate($request);
return $this->process($request);
$this->validate($params);
return $this->process($params);
}

/**
* @param array $request
* @param array $params
* @return void
*
* @throws RequestException
*/
private function validate(array $request): void
private function validate(array $params): void
{
$allowedTypes = [
self::TASK,
Expand All @@ -46,7 +45,7 @@ private function validate(array $request): void
self::M1_TASK_PROXYLESS
];

$type = $request['task']['type'] ?? '';
$type = $params['type'] ?? '';

if (!in_array($type, $allowedTypes)) {
throw new RequestException(
Expand Down

0 comments on commit 181c532

Please sign in to comment.