-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequest.php
More file actions
59 lines (49 loc) · 1.57 KB
/
Copy pathRequest.php
File metadata and controls
59 lines (49 loc) · 1.57 KB
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
<?
declare(strict_types = 1);
namespace zPHP\API;
class Request {
static $lastUrl = NULL;
/**
* @throws Exceptions\ConnError
* @throws Exceptions\ServerError
* @throws Exceptions\DataError
*/
static function get (string $domain, string $uri, array $params) : array {
self::$lastUrl = $domain . '/' . $uri . '?' . http_build_query($params);
return self::request(self::$lastUrl, stream_context_create([
'http' => ['ignore_errors' => TRUE]
]));
}
/**
* @throws Exceptions\ConnError
* @throws Exceptions\ServerError
* @throws Exceptions\DataError
*/
static function post (string $domain, string $uri, array $getParams, array $postParams) : array {
self::$lastUrl = $domain . '/' . $uri . '?' . http_build_query($getParams);
return self::request(self::$lastUrl, stream_context_create([
'http' => [
'ignore_errors' => TRUE,
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded' . PHP_EOL,
'content' => http_build_query($postParams)
]
]));
}
/**
* @throws Exceptions\ConnError
* @throws Exceptions\ServerError
* @throws Exceptions\DataError
*/
private static function request (string $url, $context) : array {
$response = @file_get_contents($url, FALSE, $context);
if (FALSE === $response)
throw new Exceptions\ConnError($url);
$json = json_decode($response, TRUE);
if (NULL === $json)
throw new Exceptions\ServerError($url, $response);
if ('error' == $json['status'])
throw new Exceptions\DataError($url, $json['message'], $json['code']);
return $json['data'];
}
}