GIVEAWAY? HOW FAIR WAS IT?


Our first Facebook giveaway was successful!  We almost reached the number we were hoping for, 92 out of 100 and we decided to run it a bit early.

How did we do it?  With the help of a computer, sheesh.  Stonge-age much?

 

This is where it gets semi-nerdy... OK, a lot nerdy.

If you're logged in to facebook with appropriate credentials you can access the facebook API.  You could develop the entire thing using OAUTH for your application to log you in, but in this case, it was much easier to access the link and save the data.

DATA URL: https://graph.facebook.com/944092175604662/likes?limit=1000

By loading up the data url in a web browser it will display the JSON data requested.  In our case, 'likes' with the page ID of 944092175604662 and declaring 1000 results limit.

I then simply save this data to be referenced later from our 'Whizbang Winner Generator'.  I named the file likes.json.

On to the 'Whizbang Winner Generator'!

Here is my PHP to read the JSON data and select a random winner, pretty sexy, I know.  The code is iffy, but I don't care, it worked.

 

<?php

$file = "likes.json";
$json = json_decode(file_get_contents($file), true);

$names = array();

// Scan through outer loop
foreach ($json as $innerArray) {
// Check type
if (is_array($innerArray)){
// Scan through inner loop
foreach ($innerArray as $value) {
$names[] = ($value['name']);
}
}
}

print_r($names);

$winner = $names[mt_rand(0, count($names) - 1)];

print("The winner of the Endy Chunky Mapley Walnuty Choppy Blocky is... {$winner}");

?>