This repository was archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi.php
More file actions
170 lines (130 loc) · 4.3 KB
/
Copy pathapi.php
File metadata and controls
170 lines (130 loc) · 4.3 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
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
<?php
require 'Zend/Json.php';
class RecipeCan_Api extends RecipeCan_Abstract {
public $response;
public function call($verb, $url, $args = array(), $headers = array()) {
set_time_limit(0);
$debug = false;
$api_key = $this->get_option('single_access_token');
if ($debug) {
echo "<b>request</b><br/>";
var_dump($args);
echo "<br/><br/>";
}
$request_url = 'http://' . $this->options['api_server'] . '/api/' .
$this->options['api_version'] . '/' . $url . '.js';
if ($api_key != '') {
$request_url .= '?user_credentials=' . $api_key;
if ($debug) {
echo "<b>api key</b><br>";
var_dump($api_key);
echo "<br><br>";
}
}
if ($debug) {
echo "<b>request url</b><br>";
echo $request_url;
echo "<br><br>";
}
// apperently some wordpress http agents dont actually
// allow delete requests. send a post request and
// attach _method=delete
if ($verb == 'delete') {
$verb = 'post';
$args['_method'] = 'delete';
}
$wp_remote_args = array(
'headers' => $headers,
'method' => strtoupper($verb),
'timeout' => 25,
'body' => $args,
);
$response = wp_remote_request($request_url, $wp_remote_args);
if ($debug) {
echo "<b>raw response</b><br/>";
var_dump($response['body']);
echo "<br/><br/>";
}
$json_as_array = Zend_Json::decode($response['body']);
if ($debug) {
echo "<b>processed response</b><br/>";
var_dump($json_as_array);
echo "<br/><br>";
}
$this->response = $json_as_array;
return $json_as_array;
}
public function failed() {
return array_key_exists('failed', $this->response);
}
public function errors() {
if ($this->failed()) {
return $this->response['error'];
} else {
return null;
}
}
public function success() {
return !$this->failed();
}
public function login($args) {
$send_args = array(
'user_session[login]' => $args['email'],
'user_session[password]' => $args['password']
);
return $this->call('post', 'users/verify', $send_args);
}
public function create_account($args) {
$args['user']['signup_source'] = "wp_plugin-" . RECIPECAN_VERSION;
return $this->call('post', 'users', $args);
}
public function user() {
return $this->call('get', 'users', array());
}
public function create_outside_blog($args) {
return $this->call('post', 'outside_blogs', $args);
}
public function update_outside_blog($args) {
return $this->call('put', 'outside_blogs/' . $args['id'], $args);
}
public function update_recipe($args) {
return $this->call('put', 'recipes/' . $args['id'], array(
'recipe' => $args
));
}
public function delete_recipe($args) {
return $this->call('delete', 'recipes/' . $args['id']);
}
public function create_recipe($args) {
return $this->call('post', 'recipes', array(
'recipe' => $args
));
}
public function create_recipe_photo($args) {
$handle = fopen($args['filename'], "rb");
$contents = fread($handle, filesize($args['filename']));
fclose($handle);
$headers = array(
'Content-Type' => 'multipart/form-data; boundary=RecipeCanUploadBound',
);
$payload = array(
"--RecipeCanUploadBound",
"Content-Disposition: form-data; name=\"photo[photo]\"; filename=\"image.jpg\"",
"Content-Transfer-Encoding: binary",
"Content-Type: image/jpeg",
"",
$contents,
"--RecipeCanUploadBound--",
);
$this->call(
'post',
'recipes/' . $args['recipe_id'] . '/photos',
implode("\r\n", $payload),
$headers
);
}
public function recipes() {
$this->call('get', 'recipes');
}
}
?>