Skip to content

Instantly share code, notes, and snippets.

@cspray
Created February 15, 2012 04:50
Show Gist options
  • Save cspray/1833311 to your computer and use it in GitHub Desktop.
Save cspray/1833311 to your computer and use it in GitHub Desktop.
A snippet of code used to gather a SO Q&A that may be deleted soon
<?php
$questionUrl = 'https://api.stackexchange.com/2.0/questions/309300?page=1&pagesize=1&order=desc&sort=votes&site=stackoverflow&filter=!LP4hJGlAKQVCM-iPRmI8NA';
$curl = curl_init($questionUrl);
curl_setopt($curl, CURLOPT_HTTPGET, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
$questionResponse = curl_exec($curl);
$answerUrl = 'https://api.stackexchange.com/2.0/answers/309622?order=desc&sort=activity&site=stackoverflow&filter=!akczUagg4t0rkU';
curl_setopt($curl, CURLOPT_URL, $answerUrl);
$answerResponse = curl_exec($curl);
$questionResponse = json_decode($questionResponse, true);
$question = $questionResponse['items'][0];
$createDate = date('Y-m-d H:i:s', $question['creation_date']);
$score = $question['score'];
$body = $question['body'];
$title = $question['title'];
$ownerName = $question['owner']['display_name'];
$ownerRep = $question['owner']['reputation'];
$ownerProfileImage = $question['owner']['profile_image'];
$ownerProfileLink = $question['owner']['link'];
$questionLink = $question['link'];
$questionHtml = <<<HTML
<div class="so-question">
<h2><a href="{$questionLink}">{$title}</a></h2>
<div class="post-content">
<div class="post-score">
{$score}
</div>
{$body}
</div>
<div class="post-meta-data">
<div class="owner-profile">
<p class="owner-image"><img alt="Gravatar for {$ownerName}" src="{$ownerProfileImage}" /></p>
<p class="owner-name"><a href="{$ownerProfileLink}">{$ownerName}</a></p>
<p class="owner-reputation">{$ownerRep}</p>
</div>
<div class="post-dates">
<p class="create-date">Created: {$createDate}</p>
<p class="edit-date"></p>
</div>
</div>
</div>
HTML;
$answerResponse = json_decode($answerResponse, true);
$answerResponse = $answerResponse['items'][0];
$answerScore = $answerResponse['score'];
$answerBody = $answerResponse['body'];
$answerDate = date('Y-m-d H:i:s', $answerResponse['creation_date']);
$answerOwner = $answerResponse['owner']['display_name'];
$answerReputation = $answerResponse['owner']['reputation'];
$answerProfileImage = $answerResponse['owner']['profile_image'];
$answerProfileLink = $answerResponse['owner']['link'];
$answerHtml = <<<HTML
<div class="so-answer">
<div class="post-content">
<div class="post-score">
{$answerScore}
</div>
{$answerBody}
</div>
<div class="post-meta-data">
<div class="owner-profile">
<p class="owner-image"><img alt="Gravatar for {$answerOwner}" src="{$answerProfileImage}" /></p>
<p class="owner-name"><a href="{$answerProfileLink}">{$answerOwner}</a></p>
<p class="owner-reputation">{$answerReputation}</p>
</div>
<div class="post-dates">
<p class="create-date">Created: {$answerDate}</p>
<p class="edit-date"></p>
</div>
</div>
</div>
HTML;
echo <<<CSS
<style type="text/css">
.so-question, .so-answer {
border: 1px solid black;
margin-bottom: .75em;
padding: 5 px;
}
</style>
CSS;
echo $questionHtml;
echo $answerHtml;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment