forked from bndr/imgur-php-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImgur.php
156 lines (139 loc) · 3.76 KB
/
Imgur.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
<?php
/**
* PHP Imgur wrapper 0.1
* Imgur API wrapper for easy use.
* @author Vadim Kr.
* @copyright (c) 2013 bndr
* @license http://creativecommons.org/licenses/by-sa/3.0/legalcode
*/
spl_autoload_register(function ($cname) {
require_once("classes/" . $cname . ".php");
throw new Exception("Class " . $cname . " failed to load. Please verify that you uploaded the files correctly.");
});
class Imgur
{
/**
* @var bool|string
*/
protected $api_key = "";
/**
* @var string
*/
protected $api_secret = "";
/**
* @var string
*/
protected $api_endpoint = "https://api.imgur.com/3";
/**
* @var string
*/
protected $oauth_endpoint = "https://api.imgur.com/oauth2";
/**
* @var Connect
*/
protected $conn;
/**
* Imgur Class constructor.
* @param string $api_key
* @param string $api_secret
* @throws
*/
function __construct($api_key, $api_secret)
{
if (!$api_key || !$api_secret) throw Exception("Please provided API key data");
$this->api_key = $api_key;
$this->api_secret = $api_secret;
$this->conn = new Connect($this->api_key, $this->api_secret);
}
/**
* oAuth2 authorization. If the acess_token needs to be refreshed pass $refresh_token as first parameter,
* if this is the first time getting access_token from user, then set the first parameter to false, pass the auth code
* in the second.
* @param bool $refresh_token
* @param bool $auth_code
* @return array $tokens
*/
function authorize($refresh_token = FALSE, $auth_code = FALSE)
{
$auth = new Authorize($this->conn, $this->api_key, $this->api_secret);
$tokens = ($refresh_token) ? $auth->refreshAccessToken($refresh_token) : $auth->getAccessToken($auth_code);
(!$tokens) ? $auth->getAuthorizationCode() : $this->conn->setAccessData($tokens['access_token'], $tokens['refresh_token']);
return $tokens;
}
/**
* Upload an image from url, bas64 string or file.
* @return mixed
*/
function upload()
{
$upload = new Upload($this->conn, $this->api_endpoint);
return $upload;
}
/**
* Image Wrapper for all image functions
* @param string $id
* @return Image
*/
function image($id = null)
{
$image = new Image($id, $this->conn, $this->api_endpoint);
return $image;
}
/**
* Album wrapper for all album functions.
* @param string $id
* @return Album
*/
function album($id = null)
{
$album = new Album($id, $this->conn, $this->api_endpoint);
return $album;
}
/**
* Account wrapper for all account functions
* @param string $username
* @return Account
*/
function account($username)
{
$acc = new Account($username, $this->conn, $this->api_endpoint);
return $acc;
}
/**
* Gallery wrapper for all functions regarding gallery
* @return Gallery
*/
function gallery()
{
$gallery = new Gallery($this->conn, $this->api_endpoint);
return $gallery;
}
/**
* Comment wrapper for all commenting functions
* @param string $id
* @return Comment
*/
function comment($id)
{
$comment = new Comment($id, $this->conn, $this->api_endpoint);
return $comment;
}
/**
* Messages wrapper
* @return Message
*/
function message()
{
$msg = new Message($this->conn, $this->api_endpoint);
return $msg;
}
/**
* Notifications wrapper
* @return mixed
*/
function notification()
{
$notification = new Notification($this->conn, $this->api_endpoint);
return $notification;
}
}