Skip to content

Instantly share code, notes, and snippets.

@chappy84 chappy84/githubStarred.php
Last active Nov 20, 2016

Embed
What would you like to do?
Github Starred Dates - displays when you starred repos, which seems to have disappeared from GitHub's UI :(
<?php
function errorAndDie($message) {
echo $message . PHP_EOL;
die(1);
}
$url = 'https://api.github.com/user/starred';
do {
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_HEADER => true,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_HTTPHEADER => [
'Accept: application/vnd.github.v3.star+json'
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => 'GitHub-Starred-Repos-Request',
CURLOPT_USERPWD => '<username>:<personal access token>',
]);
$response = curl_exec($ch);
$headerLength = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if (200 !== $httpStatusCode || false === $response) {
errorAndDie('Failed to fetch URL: ' . $url);
}
$starredRepos = json_decode(substr($response, $headerLength));
if (empty($starredRepos) || !is_array($starredRepos)) {
errorAndDie('No starred repos found on URL: ' . $url);
}
foreach ($starredRepos as $starredRepo) {
if (isset($starredRepo->repo->full_name, $starredRepo->starred_at)) {
echo $starredRepo->repo->full_name . ': ' . $starredRepo->starred_at . PHP_EOL;
}
}
$headerString = substr($response, 0, $headerLength);
$hasMorePages = false;
if (preg_match('/^\s*link:\s*(.+)\s*$/im', $headerString, $linksHeader)
&& !empty($linksHeader[1])
&& preg_match('/\<([^<>]+)\>\;\s*rel\=\"next\"/', $linksHeader[1], $nextLink)
&& !empty($nextLink[1])
) {
$url = $nextLink[1];
$hasMorePages = true;
}
} while (true === $hasMorePages);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.