Skip to content

Instantly share code, notes, and snippets.

@mheadd
Created November 3, 2012 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mheadd/4008408 to your computer and use it in GitHub Desktop.
Save mheadd/4008408 to your computer and use it in GitHub Desktop.
A simple SMS script for finding polling locations in Philadelphia.
<?php
// Declare constants for Google API Key and Civic Information API endpoint.
define("GOOGLE_API_KEY", "");
define("GOOGLE_API_URL", "https://www.googleapis.com/civicinfo/us_v1/voterinfo/4000/lookup?fields=pollingLocations&officialOnly=%official%&key=%key%");
// An extra (optional) message to tell users.
define(EXTRA_VOTER_INFORMATION, "You may be asked for an ID at polling location, but it is not required to vote.");
// A phone number to give voice callers for finding out their polling place.
define(VOTER_INFO_PHONE_NUMBER, "215-686-3460 or 215-686-3461");
// Function to make HTTP call to Google Civic Information API.
function getCivicInfo($official, $address) {
$payload = json_encode(array("address" => $address));
$url = str_replace(array('%official%', '%key%'), array($official, GOOGLE_API_KEY), GOOGLE_API_URL);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($response_code != '200') {
throw new Exception("HTTP error occured: " . $response_code);
}
return json_decode($output);
}
// Functio to format response from Google Civic Information API
function formatResponse($locations) {
$polling_location = $locations->pollingLocations[0];
$response = "Polling place: ";
$response .= $polling_location->address->locationName . ", ";
$response .= $polling_location->address->line1;
$response .= strlen($polling_location->address->line2) > 0 ? ", " . $polling_location->address->line2 : "";
$response .= strlen($polling_location->address->line3) > 0 ? ", " . $polling_location->address->line3 : "";
$response .= strlen($polling_location->address->city) > 0 ? ", " . $polling_location->address->city : "";
$response .= strlen($polling_location->address->state) > 0 ? " " . $polling_location->address->state : "";
$response .= strlen($polling_location->address->zip) > 0 ? ". " . $polling_location->address->zip : "";
$response .= strlen($polling_location->pollingHours) > 0 ? "Hours: " . $polling_location->pollingHours : "";
$response .= strlen($polling_location->notes) > 0 ? "Notes: " . $polling_location->notes : "";
return $response;
}
try {
// Tell voice callers number for finding out polling location.
if($currentCall->channel == "VOICE") {
say("To find your polling location, please hang up and call " . VOTER_INFO_PHONE_NUMBER);
}
// Get civic information for SMS users.
else {
// Format address and make API call.
$address = $currentCall->initialText;
$address .= " Philadelphia, PA";
$civicInfoResponse = getCivicInfo('true', $address);
// If success, format and send response
if($civicInfoResponse->pollingLocations) {
$response = formatResponse($civicInfoResponse);
say($response);
if(EXTRA_VOTER_INFORMATION) {
say(EXTRA_VOTER_INFORMATION);
}
}
// If invalid address, tell user to retry.
else {
say("Sorry, I couldn't find any information with the address provided. Please try again.");
}
}
}
catch (Exception $e) {
_log("*** " . $e->getMessage . " ***");
say("Sorry, there was a problem. Please try again later.");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment