Skip to content

Commit

Permalink
add guard
Browse files Browse the repository at this point in the history
  • Loading branch information
metalsadman committed Jan 20, 2023
1 parent ebd2293 commit 1eb47c2
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@
$url = 'https://secure.shippingapis.com/ShippingAPI.dll?API=Verify&XML=' . urlencode($xml);
$response = file_get_contents($url);

$xml = simplexml_load_string($response) or Utils::json_response(["message" => "USPS Response error."], 500);;
$xml = simplexml_load_string($response) or Utils::json_response(["message" => "USPS Response error."], 500);
$validatedAddress = array(
'address1' => (string)$xml->Address[0]->Address1,
'address2' => (string)$xml->Address[0]->Address2,
'city' => (string)$xml->Address[0]->City,
'state' => (string)$xml->Address[0]->State,
'zip' => (string)$xml->Address[0]->Zip5, // . '-' . $xml->Address[0]->Zip4
);
if($validatedAddress['address1'] == '' || $validatedAddress['city' == '' || $validatedAddress['zip'] == '']){
return Utils::json_response(["message" => "Invalid address!"], 400);
}
return Utils::json_response(['data' => $validatedAddress]);
}
}catch(\Exception $e){
Expand Down

0 comments on commit 1eb47c2

Please sign in to comment.