Last active
December 13, 2019 22:25
-
-
Save jarektkaczyk/b4fe2ee21d332caff65b6703a6eee828 to your computer and use it in GitHub Desktop.
PHP 'One-liner' to get Github stargazers count for a user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Not exactly one-liner but.. ;) | |
// -------------------- | |
// require: | |
// illuminate/support | |
// guzzlehttp/guzzle | |
$user = 'jarektkaczyk'; | |
$stars = collect(json_decode((new Guzzlehttp\Client)->get("https://api.github.com/users/{$user}/repos")->getBody(), true)) | |
->where('fork', false) | |
->sum('stargazers_count'); | |
// -------------------- | |
// generic PHP | |
$user = 'jarektkaczyk'; | |
curl_setopt_array($request = curl_init(), [ | |
CURLOPT_URL => "https://api.github.com/users/{$user}/repos", | |
CURLOPT_HTTPHEADER => ['User-Agent: Curl-PHP'], | |
CURLOPT_RETURNTRANSFER => true, | |
]); | |
$repos = json_decode(curl_exec($request), true); | |
curl_close($request); | |
$stars = array_reduce($repos, function ($stars, $repo) { | |
return $stars += $repo['fork'] ? 0 : $repo['stargazers_count']; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment