Created
October 9, 2010 03:17
-
-
Save quandyfactory/617837 to your computer and use it in GitHub Desktop.
PHP example using the RTH Elections API.
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | |
<html> | |
<head> | |
<title>RTH Elections Site: Using JSON in PHP</title> | |
</head> | |
<body> | |
<h1>RTH Elections Site: Using JSON in PHP</h1> | |
<?php | |
$url = 'http://elections.raisethehammer.org/api/election/1'; | |
# fetch the data from the API page | |
$response = file_get_contents($url); | |
# json_decode requires PHP 5.2+ | |
# true - decodes the JSON object into a PHP array, not a PHP object | |
$obj = json_decode($response, true); | |
$details = $obj['details']; | |
print '<h2>Details</h2>'; | |
print '<ul>'; | |
foreach ($details as $key => $detail) { | |
print '<li><strong>'.$key.'</strong>: '.$details[$key].'</li>'; | |
} | |
print '</ul>'; | |
$candidates = $obj['candidates']; | |
print '<h2>Candidates</h2>'; | |
foreach ($candidates as $candidate) { | |
print '<h3>'.$candidate['name'].'</h3>'; | |
print '<ul>'; | |
foreach ($candidate as $key => $detail) { | |
print '<li><strong>'.$key.'</strong>: '.$candidate[$key].'</li>'; | |
} | |
print '</ul>'; | |
} | |
$questions = $obj['questions']; | |
print '<h2>Questions</h2>'; | |
foreach ($questions as $question) { | |
print '<ul>'; | |
foreach ($question as $key => $detail) { | |
print '<li><strong>'.$key.'</strong>: '.$question[$key].'</li>'; | |
} | |
print '</ul>'; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment