Skip to content

Instantly share code, notes, and snippets.

@svjv1160
Last active December 10, 2021 10:02
Show Gist options
  • Save svjv1160/7749784 to your computer and use it in GitHub Desktop.
Save svjv1160/7749784 to your computer and use it in GitHub Desktop.
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