Example Client Side PHP Form

This is a very basic example of how to use this tool from PHP. Please leave comments if you have any suggestions.

<?php

$key = 'demo';
$postal_code = str_replace(' ', '', $_GET['postal_code']);


$edid = find_edid($postal_code, $key);
$mp_email = ($edid) ? find_mp_email($edid, $key) : 'Sorry, no electoral district was returned';

echo "MP's Email: $mp_email";

function find_edid($postal_code = 'M5X1J2', $key) {
  $url = "http://makethechange.ca/pc2csv/?pc={$postal_code}&key={$key}";
  $edid_string = file_get_contents($url, FALSE, NULL, 0, 200);
  $edid_clean = str_replace("'", '', trim($edid_string));
  $edid_array = explode("\n", $edid_clean); 
  $eid_count = count($edid_array);
  if ($eid_count == 1) {
    $edid_clean_array = explode(',',  $edid_clean);
    return $edid_clean_array[0];
  } elseif ($eid_count > 1) {
    foreach($edid_array AS $item) {
      $edid_array_string .= '-' .  explode(',', $item);
    }
    return $edid_array_string;
  } else {
    return false;
  }
}

function find_mp_email($edid, $key) {
  $url = "http://makethechange.ca//federal/riding.php?edid={$edid}&type=small&key={$key}";
  $mp = file_get_contents($url, FALSE, NULL, 0, 200); 
  $mp = str_replace(array("\n", '<'), array(', ', '<'), trim($mp));
  return $mp;
}

?>