Skip to content

Commit ed8632a

Browse files
committed
Better doc. Option for returning json
1 parent aaaefbd commit ed8632a

2 files changed

Lines changed: 47 additions & 31 deletions

File tree

example/api_call.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Step tree: Call the API
44

55
// Autoload
6-
include_once "../../../autoload.php";
6+
include_once "../vendor/autoload.php";
77

88
// Very small boot file. Starts session. Defines constants.
99
include_once "boot.php";
@@ -17,7 +17,7 @@
1717
// Simple call - get current users credentials
1818
// This can also be done without scope
1919
$command = "/user";
20-
$res = $api->apiCall($command);
20+
$res = $api->apiCall($command, 'GET', null);
2121
if (!$res) {
2222
print_r($api->errors); die;
2323
}

src/githubapi.php

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace diversen;
44

5-
use diversen\mycurl as mycurl;
5+
use diversen\mycurl;
66
/**
77
* contains simple class for using oauth with github
88
* @package githubapi
@@ -23,40 +23,53 @@ class githubapi {
2323
*/
2424
public $errors = array ();
2525

26-
26+
/**
27+
* @var holding return code
28+
* @var string
29+
*/
2730
public $returnCode = null;
2831
/**
29-
* we need a github OAuth login url from configuration
30-
* @param array $config e.g. <code>config = array (
31-
* 'redirect_uri' => 'http://cos/test/callback.php',
32-
* 'client_id' => 'app id',
33-
* 'state' => md5(uniqid()),
34-
* 'scope' => 'user'
35-
* );</code>
32+
* We need a github OAuth login url from configuration
33+
* @param array $config e.g.
34+
*
35+
* <code>
36+
* $config = array (
37+
* 'redirect_uri' => 'http://localhost:8080/callback.php',
38+
* 'client_id' => 'app id',
39+
* 'state' => md5(uniqid()),
40+
* 'scope' => 'user'
41+
* );
42+
* </code>
43+
*
3644
* If you don't set scope you can only get users basic profile info,
37-
* but you can use it as e.g login methdod anyway
45+
* but you can still use it as e.g. a login method.
46+
*
3847
* @return string $url a github url where you can obtain users accept of
3948
* using his account according to scope
4049
*/
4150
public function getAccessUrl ($config) {
42-
4351
$_SESSION['state'] = $config['state'];
4452
$url = 'https://github.com/login/oauth/authorize';
4553
$query = http_build_query($config);
4654
$url.= '?' . $query;
4755
return $url;
48-
4956
}
5057

5158
/**
52-
* sets the access token in a session variable, which
59+
* Sets the access token in a session variable, which
5360
* then can be used when calling the api
54-
* @param array $post e.g. <code>$post = array (
55-
* 'redirect_uri' => 'http://cos/test/callback.php',
56-
* 'client_id' => 'app_id',
57-
* 'client_secret' => 'app_secret',
58-
* );</code>
61+
* @param array $post e.g.
62+
*
63+
* <code>
64+
* $post = array (
65+
* 'redirect_uri' => 'http://localhost:8080/callback.php',
66+
* 'client_id' => 'app_id',
67+
* 'client_secret' => 'app_secret',
68+
* );
69+
* </code>
70+
*
5971
* @return boolean $res true on success and false on failure
72+
* any errors will be stored in githubapi::$errors
6073
*/
6174
public function setAccessToken ($post) {
6275
if (isset($_GET['error'])) {
@@ -87,27 +100,27 @@ public function setAccessToken ($post) {
87100
}
88101

89102
/**
90-
* Commands to call se
91-
* http://developer.github.com/v3/
92-
* For a full listing
103+
* Make an API call. For all
104+
*
105+
* @see http://developer.github.com/v3/
106+
*
93107
* @param string $command e.g "/users"
94-
* @param string $request e.g "POST" or PATCH, if empty it is a GET
108+
* @param string $request e.g "POST" or PATCH, DELETE - if empty it is a GET
95109
* @param array $post vaiables $_POST variables to send
96-
* @return array $ary response from github server
110+
* @param boolean $json should we return output as json. Default is false
111+
* @return boolean|array false if failure. Else: $ary response from github server
97112
*/
98-
public function apiCall ($command, $request = null, $post = null) {
113+
public function apiCall ($command, $request = null, $post = null, $json = false) {
99114
if (!isset($_SESSION['access_token']) || empty($_SESSION['access_token'])) {
100115
$this->errors[] = 'No valid token';
101116
return false;
102117

103118
}
104119
$end_point = 'https://api.github.com';
105120
$command = $end_point . "$command";
106-
107121
$command.= "?access_token=$_SESSION[access_token]";
108122

109123
$c = new mycurl($command);
110-
111124
if (isset($request)) {
112125
$c->setRequest($request);
113126
}
@@ -119,8 +132,11 @@ public function apiCall ($command, $request = null, $post = null) {
119132
$c->createCurl();
120133
$resp = $c->getWebPage();
121134
$this->returnCode = $c->getHttpStatus();
122-
$ary = json_decode($resp, true);
123-
return $ary;
135+
if ($json) {
136+
return $resp;
137+
} else {
138+
$ary = json_decode($resp, true);
139+
return $ary;
140+
}
124141
}
125-
126142
}

0 commit comments

Comments
 (0)