Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP example using the RTH Elections API.
<!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
You can’t perform that action at this time.