Skip to content

Instantly share code, notes, and snippets.

@cspray
Created February 14, 2012 01:25
Show Gist options
  • Save cspray/1822310 to your computer and use it in GitHub Desktop.
Save cspray/1822310 to your computer and use it in GitHub Desktop.
The code that generated the SO data for cspray.github.com/100-greatest-programming-books.html
<?php
$questionUrl = 'https://api.stackexchange.com/2.0/questions/1711/answers?site=stackoverflow&sort=votes&filter=withbody&page=1&pagesize=100';
$curl = curl_init($questionUrl);
curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
$response = curl_exec($curl);
$response = json_decode($response, true);
$css = <<<HTML
<style type="text/css">
.so-answer {
border: 1px solid black;
margin-bottom: .75em;
}
</style>
HTML;
$output = $css;
foreach ($response['items'] as $data) {
$creationDate = date('Y-m-d H:i:s', $data['creation_date']);
$lastEditDate = isset($data['last_edit_date']) ? date('Y-m-d H:i:s', $data['last_edit_date']) : 'NA';
$content = $data['body'];
$ownerName = isset($data['owner']['display_name']) ? $data['owner']['display_name'] : 'Community User';
$ownerProfileLink = isset($data['owner']['link']) ? $data['owner']['link'] : 'http://www.stackoverflow.com/users/-1/community';
$ownerImage = isset($data['owner']['profile_image']) ? $data['owner']['profile_image'] : 'http://www.gravatar.com/avatar/#';
$ownerReputation = isset($data['owner']['reputation']) ? $data['owner']['reputation'] : 0;
$answer = <<<HTML
<div class="so-answer">
<div class="answer-body">
<div class="content">{$content}</div>
</div>
<div class="answer-meta-data">
<div class="answer-owner">
<p><img src="{$ownerImage}" /><a href="{$ownerProfileLink}">{$ownerName}</a></p>
<p><span class="owner-reputation">{$ownerReputation}</span></p>
</div>
<div class="answer-dates">
<p><span class="create-date">Created on: {$creationDate}</span></p>
<p><span class="edit-date">Last edit: {$lastEditDate}</span></p>
</div>
</div>
</div>
HTML;
$output .= $answer;
}
echo $output;
echo 'Queries remaining: ' . $response['quota_remaining'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment