-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.php
More file actions
85 lines (70 loc) · 1.98 KB
/
Copy pathgithub.php
File metadata and controls
85 lines (70 loc) · 1.98 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
<?php
if (empty($_REQUEST['username'])) {
$response = [
"status" => "negative",
"message" => "Username is empty."
];
print_r(json_encode($response));
exit(1);
}
$username = $_REQUEST['username'];
function hasFollower($follower, $arr) {
$l = count($arr['nodes']);
for ($j = 0; $j < $l; $j++) {
if ($arr['nodes'][$j]['id'] == $follower->{'login'}) {
return true;
}
}
return false;
}
$opts = [
'http' => [
'method' => 'GET',
'header' => [
'User-Agent: PHP'
]
]
];
header('Content-Type: application/json');
$context = stream_context_create($opts);
if (false === ($apiResult = @file_get_contents("https://api.github.com/users/".$username, false, $context))) {
$response = [
"status" => "negative",
"message" => "Sorry, we couldn't find this username."
];
print_r(json_encode($response));
exit(1);
}
$masterUser = json_decode($apiResult);
$data = [
"nodes" => [],
"links" => []
];
$stack = [];
$z = 0;
$level = 1;
array_push($data['nodes'], ['id' => $masterUser->{'login'}, 'group' => 1]);
array_push($stack, $masterUser);
while (count($stack) > 0) {
$now = array_pop($stack);
$apiResult = file_get_contents($now->{'followers_url'}, false, $context);
$followersList = json_decode($apiResult);
$l = count($followersList);
for ($i = 0; $i < $l; $i++) {
if (!hasFollower($followersList[$i], $data)) {
if ($z < $level) {
array_push($stack, $followersList[$i]);
}
array_push($data['nodes'], ['id' => $followersList[$i]->{'login'}, 'group' => 1]);
}
array_push($data['links'], ['source' => $now->{'login'}, "target" => $followersList[$i]->{'login'},'value' => 1]);
}
$z++;
}
$response = [
"status" => "positive",
"message" => "Hey ".$masterUser->{'name'}."! Check out your great network.",
"data" => $data
];
print_r(json_encode($response));
?>