-
Notifications
You must be signed in to change notification settings - Fork 0
/
RESTClient.php
76 lines (65 loc) · 2.04 KB
/
RESTClient.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
<?php
require_once "HTTP/Request.php";
class RESTClient {
private $root_url = "";
private $curr_url = "";
private $user_name = "";
private $password = "";
private $response = "";
private $responseBody = "";
private $req = null;
public function __construct($root_url = "", $user_name = "", $password = "") {
$this->root_url = $this->curr_url = $root_url;
$this->user_name = $user_name;
$this->password = $password;
if ($root_url != "") {
$this->createRequest("GET");
$this->sendRequest();
}
return true;
}
public function createRequest($url, $method, $arr = null) {
$this->curr_url = $url;
$this->req =& new HTTP_Request($url);
if ($this->user_name != "" && $this->password != "") {
$this->req->setBasicAuth($this->user_name, $this->password);
}
switch($method) {
case "GET":
$this->req->setMethod(HTTP_REQUEST_METHOD_GET);
break;
case "POST":
$this->req->setMethod(HTTP_REQUEST_METHOD_POST);
$this->addPostData($arr);
break;
case "PUT":
$this->req->setMethod(HTTP_REQUEST_METHOD_PUT);
// to-do
break;
case "DELETE":
$this->req->setMethod(HTTP_REQUEST_METHOD_DELETE);
// to-do
break;
}
}
private function addPostData($arr) {
if ($arr != null) {
foreach ($arr as $key => $value) {
$this->req->addPostData($key, $value);
}
}
}
public function sendRequest() {
$this->response = $this->req->sendRequest();
if (PEAR::isError($this->response)) {
echo $this->response->getMessage();
die();
} else {
$this->responseBody = $this->req->getResponseBody();
}
}
public function getResponse() {
return $this->responseBody;
}
}
?>