1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the godruoyi/laravel-tencent007-captcha. |
5
|
|
|
* |
6
|
|
|
* (c) Godruoyi <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Godruoyi\Tencent007; |
12
|
|
|
|
13
|
|
|
use GuzzleHttp\Client as HttpClient; |
14
|
|
|
use RuntimeException; |
15
|
|
|
|
16
|
|
|
class Client |
17
|
|
|
{ |
18
|
|
|
const API_ADDRESS = 'https://ssl.captcha.qq.com/ticket/verify'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $appid; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $secretKey; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Register appid and secretkey. |
32
|
|
|
* |
33
|
|
|
* @param string $appid |
34
|
|
|
* @param string $secretKey |
35
|
|
|
* |
36
|
|
|
* @throws \RuntimeException |
37
|
|
|
*/ |
38
|
6 |
|
public function __construct($appid = null, $secretKey = null) |
39
|
|
|
{ |
40
|
6 |
|
$this->appid = $appid ?: config('007.appid'); |
41
|
6 |
|
$this->secretKey = $secretKey ?: config('007.secret'); |
42
|
|
|
|
43
|
6 |
|
if (!$this->appid || !$this->secretKey) { |
44
|
2 |
|
throw new RuntimeException('Please configure default appid/secret for tencent 007 in config/007.php file.'); |
45
|
|
|
} |
46
|
4 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get http client instance. |
50
|
|
|
* |
51
|
|
|
* @return \GuzzleHttp\Client |
52
|
|
|
*/ |
53
|
2 |
|
public function getHttpClient() |
54
|
|
|
{ |
55
|
2 |
|
return new HttpClient(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Static method call. |
60
|
|
|
* |
61
|
|
|
* @param string $ticket |
62
|
|
|
* @param string $randstr |
63
|
|
|
* @param string $ip |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
1 |
|
public static function check($ticket, $randstr, $ip = null) |
68
|
|
|
{ |
69
|
1 |
|
return (new static())->ticketVerify($ticket, $randstr, $ip); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Verify request ticket. |
74
|
|
|
* |
75
|
|
|
* @param string $ticket |
76
|
|
|
* @param string $randstr |
77
|
|
|
* @param string $ip |
78
|
|
|
* |
79
|
|
|
* @return \Godruoyi\Tencent007\Response |
80
|
|
|
*/ |
81
|
1 |
|
public function ticketVerify($ticket, $randstr, $ip = null) |
82
|
|
|
{ |
83
|
1 |
|
$response = $this->getHttpClient()->get(self::API_ADDRESS, [ |
84
|
|
|
'query' => [ |
85
|
1 |
|
'aid' => $this->appid, |
86
|
1 |
|
'AppSecretKey' => $this->secretKey, |
87
|
1 |
|
'Ticket' => $ticket, |
88
|
1 |
|
'Randstr' => $randstr, |
89
|
1 |
|
'UserIP' => $ip ?: request()->ip(), |
90
|
|
|
], |
91
|
|
|
]); |
92
|
|
|
|
93
|
1 |
|
$response = (string) $response->getBody(); |
94
|
|
|
|
95
|
1 |
|
$json = json_decode($response, true); |
96
|
|
|
|
97
|
1 |
|
return new Response($json); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|