Skip to content

Instantly share code, notes, and snippets.

@jarektkaczyk
Last active December 13, 2019 22:25
Show Gist options
  • Save jarektkaczyk/b4fe2ee21d332caff65b6703a6eee828 to your computer and use it in GitHub Desktop.
Save jarektkaczyk/b4fe2ee21d332caff65b6703a6eee828 to your computer and use it in GitHub Desktop.
PHP 'One-liner' to get Github stargazers count for a user
<?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