Create a gist now

Instantly share code, notes, and snippets.

This snippet shows a simple implementation of Betterplace.org API to display project progress on a custom website. The snippet uses PHP Vers. 5.2.0. and does not contain a CSS layout. For further details see Betterplace.org API: https://github.com/betterplace/betterplace_apidocs
<?php
/* Your project ID from Betterplace.org */
$project = 12345;
/* Get positive opinions (max 100) */
$json = file_get_contents('https://api.betterplace.org/en/api_v4/projects/'. $project.'/opinions.json?facets=score:positive&order=created_at:DESC&per_page=100' );
$obj = json_decode($json);
if($obj->total_entries>0) {
echo '<ul class="positive">';
foreach($obj->data as $data) {
echo '<li>'. ((isset($data->author)) ? $data->author->name : 'Anonymous person').
((isset($data->donated_amount_in_cents) && $data->donated_amount_in_cents>0) ? ' donated '.number_format(($data->donated_amount_in_cents/100), 0, '','.').'' : '' ).
((!empty($data->message)) ? $data->message : '')
.'</li>';
}
echo '</ul>';
} else {
echo '<div>No positive opinions</div>';
}
/* Get negative opinions (max 100) */
$json = file_get_contents('https://api.betterplace.org/en/api_v4/projects/'. $project.'/opinions.json?facets=score:negative&order=created_at:DESC&per_page=100' );
$obj = json_decode($json);
if($obj->total_entries>0) {
echo '<ul class="negative">';
foreach($obj->data as $data) {
echo '<li>'. ((isset($data->author)) ? $data->author->name : 'Anonymous person').
((isset($data->donated_amount_in_cents) && $data->donated_amount_in_cents>0) ? ' donated '.number_format(($data->donated_amount_in_cents/100), 0, '','.').'' : '' ).
((!empty($data->message)) ? $data->message : '')
.'</li>';
}
echo '</ul>';
} else {
echo '<div>No negative opinions</div>';
}
?>
<?php
/* Your project ID from Betterplace.org */
$project = 12345;
$json = file_get_contents('https://api.betterplace.org/en/api_v4/projects/'. $project );
$obj = json_decode($json);
?>
<p>Progress: <?php echo $obj->progress_percentage; ?> %</p>
<p>Blog posts: <?php echo $obj->blog_post_count; ?></p>
<p>Open amount: <?php echo number_format(($obj->open_amount_in_cents/100), 0, '','.'); ?> €</p>
<p>Opinions: <?php echo $obj->positive_opinions_count; ?> positive - <?php echo $obj->negative_opinions_count; ?> negative</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment