-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.php
96 lines (59 loc) · 2.71 KB
/
Client.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
<?php
namespace Bert\Flickr;
class Client {
const ACCESS_TOKEN_URL = 'http://www.flickr.com/services/oauth/access_token';
const AUTHORIZE_URL = 'http://www.flickr.com/services/oauth/authorize';
const REQUEST_TOKEN_URL = 'http://www.flickr.com/services/oauth/request_token';
const METHOD_URL = 'http://api.flickr.com/services/rest/';
private $consumer_key;
private $consumer_secret;
private $callback_url;
private $access_token = null;
public function __construct($consumer_key, $consumer_secret, $callback_url) {
$this->consumer_key = $consumer_key;
$this->consumer_secret = $consumer_secret;
$this->callback_url = $callback_url;
$this->oauth = new \OAuth($this->consumer_key, $this->consumer_secret);
}
public function getRequestToken() {
return $this->oauth->getRequestToken(self::REQUEST_TOKEN_URL, $this->callback_url);
}
public function getAuthorizeURL($token) {
return self::AUTHORIZE_URL . '?oauth_token=' . $token['oauth_token'] . '&perms=read';
}
public function setToken($token) {
$this->oauth->setToken($token['oauth_token'], $token['oauth_token_secret']);
}
public function getAccessToken($verifier) {
return $this->oauth->getAccessToken(self::ACCESS_TOKEN_URL, null, $verifier);
}
public function setAccessToken($access_token) {
$this->access_token = $access_token;
}
public function callMethod($method, $params = array()) {
try {
$params['method'] = $method;
if (!isset($params['format'])) {
$params['format'] = 'php_serial';
}
$this->oauth->enableDebug();
$this->oauth->fetch(self::METHOD_URL, $params);
$response = $this->oauth->getLastResponse();
$response_data = unserialize($response);
if ($response_data === FALSE) {
return array(
'stat' => 'fail',
'response' => $response,
'responseInfo' => $this->oauth->getLastResponseInfo(),
);
}
return $response_data;
} catch (\OAuthException $e) {
return array(
'stat' => 'fail',
'responseInfo' => $this->oauth->getLastResponseInfo(),
'exception' => $e,
);
}
}
}