1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the ReCaptcha Library. |
4
|
|
|
* |
5
|
|
|
* (c) Ilya Pokamestov <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DS\Library\ReCaptcha\Http\Client; |
12
|
|
|
|
13
|
|
|
use DS\Library\ReCaptcha\Http\Response; |
14
|
|
|
use Psr\Http\Message\RequestInterface; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ReCaptcha library, simple http file_get_content client. |
19
|
|
|
* |
20
|
|
|
* @author Ilya Pokamestov <[email protected]> |
21
|
|
|
* |
22
|
|
|
* Class SimpleClient |
23
|
|
|
* @package DS\Library\ReCaptcha\Http\Client |
24
|
|
|
*/ |
25
|
|
|
class SimpleClient implements ClientInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param RequestInterface $request |
29
|
|
|
* @return ResponseInterface |
30
|
|
|
*/ |
31
|
1 |
|
public function send(RequestInterface $request) |
32
|
|
|
{ |
33
|
1 |
|
$headers = array(); |
34
|
1 |
|
foreach ($request->getHeaders() as $name => $values) { |
35
|
1 |
|
$headers[] = sprintf('%s: %s', $name, implode(', ', $values)); |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
$options = array( |
39
|
|
|
'http' => array( |
40
|
1 |
|
'header' => implode("\r\n", $headers), |
41
|
1 |
|
'method' => (string)$request->getMethod(), |
42
|
1 |
|
'content' => (string)$request->getBody(), |
43
|
1 |
|
), |
44
|
1 |
|
); |
45
|
|
|
|
46
|
1 |
|
$response = file_get_contents( |
47
|
1 |
|
(string)$request->getUri(), |
48
|
1 |
|
false, |
49
|
1 |
|
stream_context_create($options) |
50
|
1 |
|
); |
51
|
|
|
|
52
|
1 |
|
$status = 400; |
53
|
1 |
|
$protocol = null; |
54
|
|
|
|
55
|
1 |
|
if (isset($http_response_header) && is_array($http_response_header)) { |
56
|
1 |
|
$statusLine = array_shift($http_response_header); |
57
|
1 |
|
$statusHeaderPattern = '#^HTTP/(?P<version>[1-9]\d*\.\d) (?P<status>[1-5]\d{2})(\s+(?P<reason>.+))?$#'; |
58
|
1 |
|
if (preg_match($statusHeaderPattern, $statusLine, $matches)) { |
59
|
1 |
|
$status = $matches['status']; |
60
|
1 |
|
$protocol = $matches['version']; |
61
|
1 |
|
} |
62
|
1 |
|
} |
63
|
|
|
|
64
|
1 |
|
$response = new Response($status, $http_response_header, $response === false ? null : $response); |
65
|
1 |
|
$response->withProtocolVersion($protocol); |
66
|
|
|
|
67
|
1 |
|
return $response; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|