-
Notifications
You must be signed in to change notification settings - Fork 12
/
DrupalREST.php
251 lines (204 loc) · 7.94 KB
/
DrupalREST.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/**
* Created by JetBrains PhpStorm.
* Branch project DrupalREST.php | https://github.com/RandallKent/DrupalREST.PHP
* Support CRUD Entity File, Node, entity_node, taxonomy_term | Token CSRF
* Require module services | https://drupal.org/project/services - services_entity | https://drupal.org/project/services_entity
* User: jatorres | https://github.com/jatorres
* Date: 7/10/13
* Update: 26/04/2014
*/
class DrupalREST
{
public $username;
public $password;
public $session;
public $user_drupal;
public $endpoint;
public $debug;
function __construct($domain, $endpoint, $username, $password, $debug)
{
$this->username = $username;
$this->password = $password;
//TODO: Check for trailing slash and fix if needed
$this->domain = $domain;
$this->endpoint = $endpoint;
$this->debug = $debug;
}
// Authentication drupal
function login()
{
$user_data = array(
'username' => $this->username,
'password' => $this->password,
);
$user = http_build_query($user_data, '', '&');
$ch = curl_init($this->domain . '/' . $this->endpoint . '/user/login.json');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/json",
"Content-type: application/x-www-form-urlencoded"
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE); // Ask to not return Header
curl_setopt($ch, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($ch, CURLOPT_POSTFIELDS, $user);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// Check if login was successfully
if ($http_code == 200) {
// Convert json
$logged_user = json_decode($response);
$this->user_drupal = $logged_user->user;
} else {
// Get error msg
$http_message = curl_error($ch);
die('Auth error ' . $http_message);
}
//Save Session information to be sent as cookie with future calls
$this->session = $logged_user->session_name . '=' . $logged_user->sessid;
// GET CSRF Token
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $this->domain . '/services/session/token',
));
curl_setopt($ch, CURLOPT_COOKIE, "$this->session");
$ret = new stdClass;
$ret->response = curl_exec($ch);
$ret->error = curl_error($ch);
$ret->info = curl_getinfo($ch);
$this->csrf_token = $ret->response;
}
// Retrieve a entity from a entity id
public function retrieveEntity($id, $entity = 'node')
{
//Cast Entity id as integer
$id = (int)$id;
$ch = curl_init($this->domain . '/' . $this->endpoint . '/' . $entity . '/' . $id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/json",
"Cookie: $this->session"
));
$result = $this->_handleResponse($ch);
curl_close($ch);
return $result;
}
// Create a entity
public function createEntity($fields_entity, $entity = 'node')
{
$post = http_build_query($fields_entity, '', '&');
$ch = curl_init($this->domain . '/' . $this->endpoint . '/' . $entity . '/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/json",
"Content-type: application/x-www-form-urlencoded",
"Cookie: $this->session",
"X-CSRF-Token: $this->csrf_token"
));
$result = $this->_handleResponse($ch);
curl_close($ch);
return $result;
}
// Update a entity
public function updateEntity($id, $fields_entity, $entity = 'node')
{
$id = (int)$id;
$put = http_build_query($fields_entity, '', '&');
$ch = curl_init($this->domain . '/' . $this->endpoint . '/' . $entity . '/' . $id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $put);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/json",
"Content-type: application/x-www-form-urlencoded",
"Cookie: $this->session",
'X-CSRF-Token: ' . $this->csrf_token
));
$result = $this->_handleResponse($ch);
curl_close($ch);
return $result;
}
// Delete a entity
public function deleteEntity($id, $entity = 'node')
{
$id = (int)$id;
$ch = curl_init($this->domain . '/' . $this->endpoint . '/' . $entity . '/' . $id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/json",
"Cookie: $this->session",
"X-CSRF-Token: $this->csrf_token"
));
$result = $this->_handleResponse($ch);
curl_close($ch);
return $result;
}
//Search a Entity
public function searchResource($entity, $params, $fields_result = NULL)
{
$params_url = http_build_query($params);
$ch = curl_init($this->domain . '/' . $this->endpoint . '/' . $entity . '/?' . $params_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/json",
"Cookie: $this->session"
));
$result = $this->_handleResponse($ch);
curl_close($ch);
if (is_object($result) && $result->ErrorCode === NULL) {
if ($fields_result && count($fields_result) > 0) {
$fields = array();
foreach ($fields_result as $value) {
if (isset($result->Data[$value])) {
$fields[$value] = $result->Data[$value];
}
}
$result = $fields;
} else {
$result = $result->Data;
}
} elseif (is_object($result) && $result->ErrorCode !== NULL && $fields_result && $this->debug) {
$result = $result->body;
}
return $result;
}
// Private Helper Functions
private function _handleResponse($ch)
{
$response = curl_exec($ch);
$info = curl_getinfo($ch);
//break apart header & body
$header = substr($response, 0, $info['header_size']);
$body = substr($response, $info['header_size']);
$result = new stdClass();
$node = new stdClass();
if ($info['http_code'] != '200') {
$header_arrray = explode("\n", $header);
$result->ErrorCode = $info['http_code'];
$result->ErrorText = $header_arrray['0'];
} else {
$result->ErrorCode = NULL;
$decodedBody = json_decode($body, true);
$node->Data = (isset($decodedBody[0]) ? $decodedBody[0] : $decodedBody);
$result = (object)array_merge((array)$result, (array)$node);
}
if ($this->debug) {
$result->header = $header;
$result->body = $body;
}
return $result;
}
}