Allow requesting address without separate number and street fields

This commit is contained in:
Skylar Ittner 2025-05-30 12:25:45 -06:00
parent 68bd2127e4
commit 15c5b02a7e
2 changed files with 11 additions and 4 deletions

View File

@ -65,7 +65,7 @@ $APIS = [
"logistics/uspsaddress" => [
"load" => "logistics.uspsaddress.php",
"vars" => [
"number" => "",
"number (optional)" => "",
"street" => "",
"unit (optional)" => "",
"city (optional)" => "",

View File

@ -6,7 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
$hash = sha1($VARS["number"] . "|" . ($VARS["unit"] ?? "") . "|" . $VARS["street"] . "|" . ($VARS["city"] ?? "") . "|" . ($VARS["state"] ?? "") . "|" . ($VARS["zip"] ?? ""));
$hash = sha1($VARS["number"] ?? "" . "|" . ($VARS["unit"] ?? "") . "|" . $VARS["street"] . "|" . ($VARS["city"] ?? "") . "|" . ($VARS["state"] ?? "") . "|" . ($VARS["zip"] ?? ""));
$cacheresp = $memcache->get("logistics.uspsaddress." . $hash);
if ($cacheresp !== false && empty($VARS["nocache"])) {
exitWithJson(json_decode($cacheresp, true));
@ -45,8 +45,15 @@ try {
if (!preg_match($statereg, $VARS["state"])) {
throw new Exception("Two-letter state name is required.");
}
$request = "addresses/v3/address?"
. "streetAddress=" . urlencode($VARS["number"] . " " . urldecode($VARS["street"]))
if (!empty($VARS["number"]) && !empty($VARS["street"])) {
$streetAddress = urldecode($VARS["number"]) . " " . urldecode($VARS["street"]);
} else if (!empty($VARS["street"])) {
$streetAddress = urldecode($VARS["street"]);
} else {
$streetAddress = "";
}
$request = "addresses/v3/address?streetAddress=" . urlencode($streetAddress)
. (!empty($VARS["unit"]) ? "&secondaryAddress=" . urlencode(urldecode($VARS["unit"])) : "")
. (!empty($VARS["city"]) ? "&city=" . urlencode(urldecode($VARS["city"])) : "&city=")
. (!empty($VARS["state"]) ? "&state=" . urlencode(urldecode($VARS["state"])) : "&state=")