forked from RandallKent/DrupalREST.PHP
-
Notifications
You must be signed in to change notification settings - Fork 2
/
DrupalREST.php
104 lines (91 loc) · 3.39 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
<?php
class DrupalREST {
var $username;
var $password;
var $session;
var $endpoint;
var $debug;
function __construct($endpoint, $username, $password, $debug) {
$this->username = $username;
$this->password = $password;
if ((substr($endpoint, -1) != "/")) {
//no trailing slash! Add one!
$endpoint .= "/";
}
$this->endpoint = $endpoint;
$this->debug = $debug;
}
function login() {
$ch = curl_init($this->endpoint . 'user/login/');
$post_data = array(
'username' => $this->username,
'password' => $this->password,
);
$post = json_encode($post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/json",
"Content-type: application/json"
));
$response = json_decode(curl_exec($ch));
curl_close($ch);
//Save Session information to be sent as cookie with future calls
$this->session = $response->session_name . '=' . $response->sessid;
}
// Retrieve a node from a node id
function retrieveNode($nid) {
//Cast node id as integer
$nid = (int) $nid;
$ch = curl_init($this->endpoint . 'node/' . $nid . '/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 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;
}
function createNode($node_json) {
$ch = curl_init($this->endpoint . 'node/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE); // Ask to not return Header
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $node_json);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($ch, CURLOPT_COOKIE, "$this->session"); // use the previously saved session
curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
$result = $this->_handleResponse($ch);
curl_close($ch);
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();
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);
$result = (object) array_merge((array) $result, (array) $decodedBody);
}
if ($this->debug) {
$result->header = $header;
$result->body = $body;
}
return $result;
}
}
?>