-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuRuApi.php
60 lines (47 loc) · 1.22 KB
/
AuRuApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace INSafonov;
use INSafonov\AuRuApi\Exception;
class AuRuApi
{
protected $token;
public $baseUrl;
public function sendRequest($requestData)
{
$method = $requestData['method'] ?: 'GET';
$headers = [];
foreach ($requestData['headers'] as $i => $header) {
$headers[] = "$i: $header";
}
$opts = [
'http' => [
'method' => $method,
'header' => implode("\r\n", $headers),
'ignore_errors' => true,
]
];
if (array_key_exists('content', $requestData))
$opts['http']['content'] = $requestData['content'];
$context = stream_context_create($opts);
$response = @file_get_contents("$this->baseUrl/" . ltrim($requestData['path'], '/'), false, $context);
$code = $this->getResponseCode($http_response_header);
if ($code === false) {
throw new Exception(error_get_last());
} else {
return [$code, $response];
}
}
protected function getResponseCode($headers)
{
for ($i = count($headers) - 1; $i >= 0; $i--) {
$matches = [];
if (preg_match('#^HTTP/\d+\.\d+\s+(\d{3})#', $headers[$i], $matches))
return (int)$matches[1];
}
return false;
}
public function __construct($params = [])
{
if (array_key_exists('token', $params))
$this->token = $params['token'];
}
}