Repository navigation
Expand file tree
/
Copy pathgithubapi.php
More file actions
126 lines (107 loc) · 3.85 KB
/
Copy pathgithubapi.php
File metadata and controls
126 lines (107 loc) · 3.85 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
<?php
namespace diversen;
use diversen\mycurl as mycurl;
/**
* contains simple class for using oauth with github
* @package githubapi
*/
/**
* Simple class for using the github oauth api:
*
*
*
*
* @package githubapi
*/
class githubapi {
/**
*
* @var array $errors holding errors
*/
public $errors = array ();
public $returnCode = null;
/**
* we need a github OAuth login url from configuration
* @param array $config e.g. <code>config = array (
* 'redirect_uri' => 'http://cos/test/callback.php',
* 'client_id' => 'app id',
* 'state' => md5(uniqid()),
* 'scope' => 'user'
* );</code>
* If you don't set scope you can only get users basic profile info,
* but you can use it as e.g login methdod anyway
* @return string $url a github url where you can obtain users accept of
* using his account according to scope
*/
public function getAccessUrl ($config) {
$_SESSION['state'] = $config['state'];
$url = 'https://github.com/login/oauth/authorize';
$query = http_build_query($config);
$url.= '?' . $query;
return $url;
}
/**
* sets the access token in a session variable, which
* then can be used when calling the api
* @param array $post e.g. <code>$post = array (
* 'redirect_uri' => 'http://cos/test/callback.php',
* 'client_id' => 'app_id',
* 'client_secret' => 'app_secret',
* );</code>
* @return boolean $res true on success and false on failure
*/
public function setAccessToken ($post) {
if (isset($_GET['error'])) {
$this->errors[] = $_GET['error'];
return false;
}
if (isset($_GET['code'])) {
$c = new mycurl('https://github.com/login/oauth/access_token');
$post['code'] = $_GET['code'];
$post['state'] = $_SESSION['state'];
$c->setPost($post);
$c->createCurl();
$resp = $c->getWebPage();
parse_str($resp, $ary);
if (isset($ary['access_token']) && isset($ary['token_type']) && $ary['token_type'] == 'bearer') {
$_SESSION['access_token'] = $ary['access_token'];
return true;
} else {
$this->errors[] = "No access token returned";
return false;
}
}
return false;
}
/**
* Commands to call se
* http://developer.github.com/v3/
* For a full listing
* @param string $command e.g "/users"
* @param string $request e.g "POST" or PATCH, if empty it is a GET
* @param array $post vaiables $_POST variables to send
* @return array $ary response from github server
*/
public function apiCall ($command, $request = null, $post = null) {
if (!isset($_SESSION['access_token']) || empty($_SESSION['access_token'])) {
$this->errors[] = 'No valid token';
return false;
}
$end_point = 'https://api.github.com';
$command = $end_point . "$command";
$command.= "?access_token=$_SESSION[access_token]";
$c = new mycurl($command);
if (isset($request)) {
$c->setRequest($request);
}
if (isset($post)) {
$json = json_encode($post);
$c->setPost($json);
}
$c->createCurl();
$resp = $c->getWebPage();
$this->returnCode = $c->getHttpStatus();
$ary = json_decode($resp, true);
return $ary;
}
}