Skip to content

Commit 64125b8

Browse files
committed
Initial commit
0 parents  commit 64125b8

12 files changed

+1409
-0
lines changed

Imgur.php

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
/**
3+
* PHP Imgur wrapper 0.1
4+
* Imgur API wrapper for easy use.
5+
* @author Vadim Kr.
6+
* @copyright (c) 2013 bndr
7+
* @license http://creativecommons.org/licenses/by-sa/3.0/legalcode
8+
*/
9+
10+
spl_autoload_register(function ($cname) {
11+
require_once("classes/" . $cname . ".php");
12+
throw new Exception("Class " . $cname . " failed to load. Please verify that you uploaded the files correctly.");
13+
});
14+
15+
class Imgur
16+
{
17+
18+
/**
19+
* @var bool|string
20+
*/
21+
protected $api_key = "df2a8a594ed512f";
22+
/**
23+
* @var string
24+
*/
25+
protected $api_secret = "a6ee7218a80b7e41d434c50b6726dc6bbd83329c";
26+
/**
27+
* @var string
28+
*/
29+
protected $api_endpoint = "https://api.imgur.com/3";
30+
/**
31+
* @var string
32+
*/
33+
protected $oauth_endpoint = "https://api.imgur.com/oauth2";
34+
/**
35+
* @var Connect
36+
*/
37+
protected $conn;
38+
39+
/**
40+
* Imgur Class constructor.
41+
* @param $api_key
42+
* @param $api_secret
43+
*/
44+
function __construct($api_key = false, $api_secret = false)
45+
{
46+
if ($api_key) $this->api_key = $api_key;
47+
if ($api_secret) $this->api_key = $api_secret;
48+
$this->conn = new Connect($this->api_key, $this->api_secret);
49+
}
50+
51+
/**
52+
* oAuth2 authorization. If the acess_token needs to be refreshed pass $refresh_token as first parameter,
53+
* if this is the first time getting access_token from user, then set the first parameter to false, pass the auth code
54+
* in the second.
55+
* @param bool $refresh_token
56+
* @param bool $auth_code
57+
* @return array $tokens
58+
*/
59+
function authorize($refresh_token = FALSE, $auth_code = FALSE)
60+
{
61+
$tokens = null;
62+
$auth = new Authorize($this->conn, $this->api_key, $this->api_secret);
63+
if ($auth_code) { // Authorization code was passed, exchanging it for access_token
64+
$tokens = $auth->getAccessToken($auth_code);
65+
$this->conn->setAccessData($tokens['access_token'], $tokens['refresh_token']);
66+
} else if ($refresh_token) { // Already have refresh_token, exchanging it for access_token
67+
$tokens = $auth->refreshAccessToken($refresh_token);
68+
$this->conn->setAccessData($tokens['access_token'], $tokens['refresh_token']);
69+
} else {
70+
$auth->getAuthorizationCode(); // Show user the authentication form
71+
}
72+
73+
return $tokens;
74+
75+
}
76+
77+
/**
78+
* Upload an image from url, bas64 string or file.
79+
* @return mixed
80+
*/
81+
function upload()
82+
{
83+
$upload = new Upload($this->conn, $this->api_endpoint);
84+
return $upload;
85+
}
86+
87+
/**
88+
* Image Wrapper for all image functions
89+
* @param string $id
90+
* @return Image
91+
*/
92+
function image($id = null)
93+
{
94+
95+
$image = new Image($id, $this->conn, $this->api_endpoint);
96+
return $image;
97+
}
98+
99+
/**
100+
* Album wrapper for all album functions.
101+
* @param string $id
102+
* @return Album
103+
*/
104+
function album($id = null)
105+
{
106+
$album = new Album($id, $this->conn, $this->api_endpoint);
107+
return $album;
108+
}
109+
110+
/**
111+
* Account wrapper for all account functions
112+
* @param string $username
113+
* @return Account
114+
*/
115+
function account($username)
116+
{
117+
$acc = new Account($username, $this->conn, $this->api_endpoint);
118+
return $acc;
119+
}
120+
121+
/**
122+
* Gallery wrapper for all functions regarding gallery
123+
* @return Gallery
124+
*/
125+
function gallery()
126+
{
127+
$gallery = new Gallery($this->conn, $this->api_endpoint);
128+
return $gallery;
129+
}
130+
131+
/**
132+
* Comment wrapper for all commenting functions
133+
* @param string $id
134+
* @return Comment
135+
*/
136+
function comment($id)
137+
{
138+
$comment = new Comment($id, $this->conn, $this->api_endpoint);
139+
return $comment;
140+
}
141+
142+
function message()
143+
{
144+
$msg = new Message($this->conn, $this->api_endpoint);
145+
return $msg;
146+
}
147+
148+
}
149+
150+
if (isset($_GET['code'])) {
151+
$imgur = new Imgur();
152+
$imgur->authorize(false, $_GET['code']);
153+
154+
$acc = $imgur->account("tehlulz")->stats();
155+
var_dumP($acc);
156+
157+
} else {
158+
$imgur = new Imgur();
159+
$imgur->authorize();
160+
}

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Imgur API PHP Wrapper 0.1
2+
=========================
3+
Easy to user php wrapper for imgur API. You can see example usage in examples.php
4+
5+
Notes
6+
=====
7+
This is a work in progress. There might be bugs.
8+
9+
Changelog
10+
=========
11+
0.1 Initial release

classes/Account.php

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
/**
3+
* PHP Imgur wrapper 0.1
4+
* Imgur API wrapper for easy use.
5+
* @author Vadim Kr.
6+
* @copyright (c) 2013 bndr
7+
* @license http://creativecommons.org/licenses/by-sa/3.0/legalcode
8+
*/
9+
10+
11+
class Account
12+
{
13+
14+
protected $conn;
15+
protected $endpoint;
16+
protected $username;
17+
18+
/**
19+
* Constructor
20+
* @param string $username
21+
* @param Connection $connection
22+
* @param string $endpoint
23+
*/
24+
function __construct($username, $connection, $endpoint)
25+
{
26+
$this->conn = $connection;
27+
$this->endpoint = $endpoint;
28+
$this->username = $username;
29+
}
30+
31+
/**
32+
* Get basic information about the user
33+
* @return mixed
34+
*/
35+
function basic()
36+
{
37+
$uri = $this->endpoint . "/account/" . $this->username;
38+
var_dump($uri);
39+
return $this->conn->request($uri);
40+
}
41+
42+
/**
43+
* Create user. $options are used as post_fields
44+
* @param array $options
45+
* @return array mixed
46+
*/
47+
function create($options)
48+
{
49+
$uri = $this->endpoint . "/account/" . $this->username;
50+
51+
return $this->conn->request($uri, $options, "POST");
52+
}
53+
54+
/**
55+
* @return mixed
56+
*/
57+
function delete()
58+
{
59+
$uri = $this->endpoint . "/account/" . $this->username;
60+
61+
return $this->conn->request($uri, array("delete" => true), "DELETE");
62+
}
63+
64+
/**
65+
* @param $type
66+
* @return mixed
67+
*/
68+
function favorites($type)
69+
{
70+
$uri = $this->endpoint . "/account/" . $this->username . ($type == 'gallery' ? "/gallery_favorites" : "/favorites");
71+
72+
return $this->conn->request($uri);
73+
}
74+
75+
/**
76+
* @param int $page
77+
* @return mixed
78+
*/
79+
function submissions($page = 0)
80+
{
81+
$uri = $this->endpoint . "/account/" . $this->username . "/submissions/" . $page;
82+
83+
return $this->conn->request($uri);
84+
}
85+
86+
/**
87+
* @param bool $options
88+
* @return mixed
89+
*/
90+
function settings($options = FALSE)
91+
{
92+
$uri = $this->endpoint . "/account/" . $this->username . "/settings";
93+
94+
return $this->conn->request($uri, $options, ($options == FALSE) ? "GET" : "POST");
95+
}
96+
97+
/**
98+
* @return mixed
99+
*/
100+
function stats()
101+
{
102+
$uri = $this->endpoint . "/account/" . $this->username . "/stats";
103+
104+
return $this->conn->request($uri);
105+
}
106+
107+
/**
108+
* @param int $page
109+
* @return mixed
110+
*/
111+
function albums($page = 0)
112+
{
113+
$uri = $this->endpoint . "/account/" . $this->username . "/albums/" . $page;
114+
115+
return $this->conn->request($uri);
116+
}
117+
118+
/**
119+
* @param int $page
120+
* @return mixed
121+
*/
122+
function images($page = 0)
123+
{
124+
$uri = $this->endpoint . "/account/" . $this->username . "/images/" . $page;
125+
126+
return $this->conn->request($uri);
127+
}
128+
129+
/**
130+
* @param $new
131+
* @return mixed
132+
*/
133+
function notifications($new)
134+
{
135+
$uri = $this->endpoint . "/account/" . $this->username . "/notifications?new=" . ($new == true) ? "true" : "false";
136+
137+
return $this->conn->request($uri);
138+
}
139+
140+
/**
141+
* @param $new
142+
* @return mixed
143+
*/
144+
function messages($new)
145+
{
146+
$uri = $this->endpoint . "/account/" . $this->username . "/notifications/messages?new=" . ($new == true) ? "true" : "false";
147+
148+
return $this->conn->request($uri);
149+
}
150+
151+
/**
152+
* @param $options
153+
* @return mixed
154+
*/
155+
function send_message($options)
156+
{
157+
$uri = $this->endpoint . "/account/" . $this->username . "/message";
158+
159+
return $this->conn->request($uri, $options, "POST");
160+
}
161+
162+
/**
163+
* @param $new
164+
* @return mixed
165+
*/
166+
function replies($new)
167+
{
168+
$uri = $this->endpoint . "/account/" . $this->username . "/notifications/replies?new=" . ($new == true) ? "true" : "false";
169+
170+
return $this->conn->request($uri);
171+
172+
}
173+
174+
175+
}
176+

0 commit comments

Comments
 (0)