forked from shurikk/rest-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest_client.php
101 lines (85 loc) · 3.08 KB
/
rest_client.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
class HttpServerException extends Exception {
}
class HttpServerException404 extends Exception {
function __construct($message = 'Not Found') {
parent::__construct($message, 404);
}
}
class RestClientException extends Exception {
}
class RestClient {
const COOKIE_JAR = '/tmp/rest-client-cookie';
const AGENT = 'rest-client-php/0.0.2';
public $response_info;
public $response_object;
public $response_raw;
private $base_url = '';
public $http_options = array();
function __construct($base_url = '', $http_options = array()) {
$this->base_url = $base_url;
$this->http_options = array_merge(array(
'cookiestore' => self::COOKIE_JAR,
'useragent' => self::AGENT,
'redirect' => 5
), $http_options);
}
function get($url, $http_options = array()) {
$url = "$this->base_url" . "$url";
$url = rtrim($url, '/'); // strip trailing slash
$http_options = array_merge($this->http_options, $http_options);
$this->http_parse_message(
http_get($url, $http_options, $this->response_info)
);
return $this->response_object->body;
}
function post($url, $fields = array(), $http_options = array()) {
$url = "$this->base_url" . "$url";
$url = rtrim($url, '/'); // strip trailing slash
$http_options = array_merge($this->http_options, $http_options);
$res = is_array($fields) ?
http_post_fields($url, $fields, array(), $http_options, $this->response_info) :
http_post_data($url, $fields, $http_options, $this->response_info);
$this->http_parse_message($res);
return $this->response_object->body;
}
function put($url, $data = '', $http_options = array()) {
$url = "$this->base_url" . "$url";
$url = rtrim($url, '/'); // strip trailing slash
$http_options = array_merge($this->http_options, $http_options);
$this->http_parse_message(
http_put_data($url, $data, $http_options, $this->response_info)
);
return $this->response_object->body;
}
function delete($url, $http_options = array()) {
$url = "$this->base_url" . "$url";
$url = rtrim($url, '/'); // strip trailing slash
$http_options = array_merge($this->http_options, $http_options);
$this->http_parse_message(
http_request(HTTP_METH_DELETE, $url, '', $http_options, $this->response_info)
);
return $this->response_object->body;
}
function http_parse_message($res) {
$this->response_raw = $res;
$this->response_object = http_parse_message($res);
if($this->response_object->responseCode == 404) {
throw new HttpServerException404(
$this->response_object->responseStatus
);
}
if($this->response_object->responseCode >= 400 && $this->response_object->responseCode <=600) {
throw new HttpServerException(
$this->response_object->responseStatus,
$this->response_object->responseCode
);
}
if(!in_array($this->response_object->responseCode, range(200,207))) {
throw new RestClientException(
$this->response_object->responseStatus,
$this->response_object->responseCode
);
}
}
}