Beispiel #1
0
require 'simple_html_dom.php';
function validate_zip($zip)
{
    return preg_match('/^[0-9]{5}([- ]?[0-9]{4})?$/', $zip);
}
class customException extends Exception
{
    public function errorMessage()
    {
        $errorMsg = 'Error on line ' . $this->getLine() . ' in ' . $this->getFile() . ': <b>' . $this->getMessage() . '</b> is not a valid zipcode';
        return $errorMsg;
    }
}
$zip = file_get_contents("php://input");
try {
    if (validate_zip($zip) === FALSE) {
        throw new customException($email);
    }
} catch (customException $e) {
    echo $e->errorMessage();
}
$url = 'http://www.motortrend.com/gas_prices/34/' . $zip;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
$result = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($httpCode == 404) {
    echo "404";
} else {
// Assign POST data
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$country = $_POST['country'];
// Validate POST data
$b_is_first_name_valid = validate_string($first_name);
$b_is_last_name_valid = validate_string($last_name);
$b_is_address_valid = validate_address($address1, $address2);
$b_is_city_valid = validate_city($city);
$b_is_state_valid = validate_string($state);
$b_is_zip_valid = validate_zip($zip);
$b_is_country_valid = validate_string($country);
// Verify required fields have been populated with valid input.
if ($b_is_first_name_valid == true && $b_is_last_name_valid == true && $b_is_address_valid == true && $b_is_city_valid == true && $b_is_state_valid == true && $b_is_zip_valid == true && $b_is_country_valid == true) {
    // Create array of data to submit.
    $data = array('first_name' => $first_name, 'last_name' => $last_name, 'address1' => $address1, 'address2' => $address2, 'city' => $city, 'state' => $state, 'zip' => $zip, 'country' => $country);
    // Submit data to database.
    $b_rc = submit_to_database($data);
    if ($b_rc == true) {
        // Print html and body tags.
        echo "<html>\n";
        echo "<body>\n";
        // Show registration confirmation message.
        echo "Thanks for registering!<br>\n";
        // Close body and html tags.
        echo "</body>\n";