-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPinterest.php
185 lines (149 loc) · 5 KB
/
Pinterest.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
<?php
/**
* Description of Pinterest
*
* @author Julian Margara <[email protected]>
*/
class Pinterest {
const API_BASE = 'https://api.pinterest.com/v1/';
const OAUTH_BASE = 'https://api.pinterest.com/oauth/';
private $client_id = '';
private $client_secret = '';
private $access_token;
function __construct($access_token = false) {
if ($access_token) {
$this->access_token = $access_token;
}
}
function getAccess_token() {
return $this->access_token;
}
function setAccess_token($access_token) {
$this->access_token = $access_token;
}
public function getLoginUrl(array $scope = array(), $redirect_uri = false, $state = false) {
if ($scope) {
$scope = implode(',', $scope);
}
if (!$state) {
$state = uniqid('', true);
}
$params = array(
'response_type' => 'token',
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'state' => $state,
'scope' => $scope,
'redirect_uri' => $redirect_uri,
);
$url = self::OAUTH_BASE . '?' . http_build_query($params);
return $url;
}
public function getUser($user_id = false, $fields = array()) {
if (!$fields) {
$fields = array('id', 'username', 'first_name', 'last_name', 'image');
}
if ($user_id) {
return $this->get('users/' . $user_id, $fields);
} else {
return $this->get('me', $fields);
}
}
public function getBoards($fields = array()) {
if (!$fields) {
$fields = array('id', 'name');
}
return $this->get('me/boards', $fields);
}
public function getBoard($board_id, $fields = array()) {
if (!$fields) {
$fields = array('id', 'name');
}
return $this->get('boards/' . $board_id, $fields);
}
public function getBoardPins($board_id, $fields = array()) {
if (!$fields) {
$fields = array('id', 'note', 'image(original)');
}
return $this->get('boards/' . $board_id . '/pins', $fields);
}
public function getPins($fields = array()) {
if (!$fields) {
$fields = array('id', 'note', 'image(original)', 'board(id,name)');
}
return $this->get('me/pins', $fields);
}
public function getPin($pin_id, $fields = array()) {
if (!$fields) {
$fields = array('id', 'note', 'image(original)');
}
return $this->get('pins/' . $pin_id, array('id', 'note', 'image(original)'));
}
public function createPin($board_id, $message, $url, $image) {
$params = array(
'board' => $board_id,
'note' => $message,
'link' => $url,
);
if (is_string($image)) {
$params['image_url'] = $image;
} else {
$params['image'] = $image;
}
return $this->post('pins', $params);
}
public function createBoard($name, $description) {
$params = array(
'name' => $name,
'description' => $description,
);
return $this->post('boards', $params);
}
public function deletePin($pin_id) {
return $this->delete('pins/' . $pin_id);
}
public function deleteBoard($board_id) {
return $this->delete('boards/' . $board_id);
}
private function post($endpoint, $params = array()) {
return $this->request($endpoint, $params, 'POST');
}
private function get($endpoint, $fields = '', $params = array()) {
if (is_array($fields)) {
$params['fields'] = implode(',', $fields);
}
return $this->request($endpoint, $params, 'GET');
}
private function delete($endpoint, $params = array()) {
return $this->request($endpoint, $params, 'DELETE');
}
private function request($endpoint, $params, $method) {
$ch = curl_init();
$request_url = self::API_BASE . $endpoint;
if ($this->access_token) {
$request_url .= '?access_token=' . $this->access_token;
}
switch ($method) {
case 'POST':
curl_setopt($ch, CURLOPT_POST, true);
case 'GET':
case 'DELETE':
$request_url .= '&' . http_build_query($params);
break;
}
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if(array_key_exists('data', $data)) {
return $data['data'];
} else {
return array('error' => $data);
}
}
}