Create API
This commit is contained in:
commit
79ec05a7b7
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
vendor/
|
13
composer.json
Normal file
13
composer.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"name": "vendor/address-autocomplete-a-p-i",
|
||||||
|
"description": "Description of project AddressAutocompleteAPI.",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Skylar Ittner",
|
||||||
|
"email": "skylar@netsyms.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"require": {
|
||||||
|
"catfan/medoo": "9999999-dev"
|
||||||
|
}
|
||||||
|
}
|
98
composer.lock
generated
Normal file
98
composer.lock
generated
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
{
|
||||||
|
"_readme": [
|
||||||
|
"This file locks the dependencies of your project to a known state",
|
||||||
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
|
"This file is @generated automatically"
|
||||||
|
],
|
||||||
|
"content-hash": "266fbccdaf4354fbcc399e2b79682a5e",
|
||||||
|
"packages": [
|
||||||
|
{
|
||||||
|
"name": "catfan/medoo",
|
||||||
|
"version": "dev-master",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/catfan/Medoo.git",
|
||||||
|
"reference": "b49a620cf1ad0d2e90ebd9f85b82aa11271df5f4"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/catfan/Medoo/zipball/b49a620cf1ad0d2e90ebd9f85b82aa11271df5f4",
|
||||||
|
"reference": "b49a620cf1ad0d2e90ebd9f85b82aa11271df5f4",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"ext-pdo": "*",
|
||||||
|
"php": ">=7.3"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^9.0"
|
||||||
|
},
|
||||||
|
"suggest": {
|
||||||
|
"ext-pdo_dblib": "For MSSQL or Sybase database on Linux/UNIX platform",
|
||||||
|
"ext-pdo_mysql": "For MySQL or MariaDB database",
|
||||||
|
"ext-pdo_oci": "For Oracle database",
|
||||||
|
"ext-pdo_pqsql": "For PostgreSQL database",
|
||||||
|
"ext-pdo_sqlite": "For SQLite database",
|
||||||
|
"ext-pdo_sqlsrv": "For MSSQL database on both Window/Liunx platform"
|
||||||
|
},
|
||||||
|
"default-branch": true,
|
||||||
|
"type": "framework",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Medoo\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Angel Lai",
|
||||||
|
"email": "angel@medoo.in"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "The lightweight PHP database framework to accelerate development",
|
||||||
|
"homepage": "https://medoo.in",
|
||||||
|
"keywords": [
|
||||||
|
"database",
|
||||||
|
"database library",
|
||||||
|
"lightweight",
|
||||||
|
"mariadb",
|
||||||
|
"mssql",
|
||||||
|
"mysql",
|
||||||
|
"oracle",
|
||||||
|
"php framework",
|
||||||
|
"postgresql",
|
||||||
|
"sql",
|
||||||
|
"sqlite"
|
||||||
|
],
|
||||||
|
"support": {
|
||||||
|
"issues": "https://github.com/catfan/Medoo/issues",
|
||||||
|
"source": "https://github.com/catfan/Medoo"
|
||||||
|
},
|
||||||
|
"funding": [
|
||||||
|
{
|
||||||
|
"url": "https://paypal.me/AngelaonLai",
|
||||||
|
"type": "custom"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"url": "https://opencollective.com/medoo",
|
||||||
|
"type": "open_collective"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"time": "2023-01-20T09:30:24+00:00"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"packages-dev": [],
|
||||||
|
"aliases": [],
|
||||||
|
"minimum-stability": "stable",
|
||||||
|
"stability-flags": {
|
||||||
|
"catfan/medoo": 20
|
||||||
|
},
|
||||||
|
"prefer-stable": false,
|
||||||
|
"prefer-lowest": false,
|
||||||
|
"platform": [],
|
||||||
|
"platform-dev": [],
|
||||||
|
"plugin-api-version": "2.0.0"
|
||||||
|
}
|
138
index.php
Normal file
138
index.php
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||||
|
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/EmptyPHP.php to edit this template
|
||||||
|
*/
|
||||||
|
|
||||||
|
$SETTINGS = [
|
||||||
|
"debugmode" => true,
|
||||||
|
"require_database" => true,
|
||||||
|
"result_limit" => 20,
|
||||||
|
"database" => [
|
||||||
|
"database_type" => "mysql",
|
||||||
|
"database_name" => "address_autocomplete",
|
||||||
|
"server" => "localhost",
|
||||||
|
"username" => "root",
|
||||||
|
"password" => "",
|
||||||
|
"charset" => "utf8"
|
||||||
|
],
|
||||||
|
"memcached" => [
|
||||||
|
"enable" => false,
|
||||||
|
"server" => "127.0.0.1",
|
||||||
|
"port" => 11211,
|
||||||
|
"prefix" => "aa"
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
function env(string $key, $defaultvalue = null) {
|
||||||
|
global $SETTINGS;
|
||||||
|
if (!empty($SETTINGS[$key])) {
|
||||||
|
return $SETTINGS[$key];
|
||||||
|
}
|
||||||
|
return $defaultvalue;
|
||||||
|
}
|
||||||
|
|
||||||
|
function envhas(string $key): bool {
|
||||||
|
global $SETTINGS;
|
||||||
|
return !empty($SETTINGS[$key]);
|
||||||
|
}
|
||||||
|
|
||||||
|
ob_start(); // allow sending headers after content
|
||||||
|
// Unicode, solves almost all stupid encoding problems
|
||||||
|
header('Content-Type: application/json; charset=utf-8');
|
||||||
|
header('X-Powered-By: PHP');
|
||||||
|
header("Access-Control-Allow-Origin: *");
|
||||||
|
|
||||||
|
//
|
||||||
|
// Composer
|
||||||
|
require __DIR__ . '/vendor/autoload.php';
|
||||||
|
|
||||||
|
use Medoo\Medoo;
|
||||||
|
|
||||||
|
$database;
|
||||||
|
try {
|
||||||
|
$database = new Medoo(env("database", []));
|
||||||
|
} catch (Exception $ex) {
|
||||||
|
if (env("require_database")) {
|
||||||
|
http_response_code(500);
|
||||||
|
exit('{"status": "ERROR", "msg": "Database error."}');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$memcacheconfig = env("memcached", [
|
||||||
|
"enable" => false,
|
||||||
|
"server" => "127.0.0.1",
|
||||||
|
"port" => 11211,
|
||||||
|
"prefix" => ""
|
||||||
|
]);
|
||||||
|
$memcache = new MemcacheDriver($memcacheconfig["enable"], $memcacheconfig["server"], $memcacheconfig["port"], $memcacheconfig["prefix"]);
|
||||||
|
unset($memcacheconfig);
|
||||||
|
|
||||||
|
if (env("debugmode", false)) {
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
ini_set('display_errors', 'On');
|
||||||
|
} else {
|
||||||
|
error_reporting(0);
|
||||||
|
ini_set('display_errors', 'Off');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($_REQUEST["address"])) {
|
||||||
|
exit(json_encode(["status" => "ERROR", "message" => "Empty address"]));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($_REQUEST["zip"])) {
|
||||||
|
exit(json_encode(["status" => "ERROR", "message" => "Empty ZIP"]));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (preg_match("/[0-9]{5}-?[0-9]{4}", $_REQUEST["zip"])) {
|
||||||
|
$_REQUEST["zip"] = substr($_REQUEST["zip"], 0, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
$addressparts = explode(" ", $_REQUEST["address"], 2);
|
||||||
|
|
||||||
|
if (count($addressparts) < 1 || !is_numeric($addressparts[0])) {
|
||||||
|
exit(json_encode(["status" => "OK", "results" => []]));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (count($addressparts) == 1) {
|
||||||
|
$results = $database->select("addresses",
|
||||||
|
["number", "street", "city", "state", "zipcode (zip)", "plus4"],
|
||||||
|
[
|
||||||
|
"AND" => ["zipcode[~]" => $_REQUEST["zip"] . '%', "number[~]" => $addressparts[0] . "%"],
|
||||||
|
"LIMIT" => env("result_limit", 20),
|
||||||
|
"ORDER" => ["updated"]
|
||||||
|
]
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$results = $database->select("addresses",
|
||||||
|
["number", "street", "city", "state", "zipcode (zip)", "plus4"],
|
||||||
|
[
|
||||||
|
"AND" => ["zipcode[~]" => $_REQUEST["zip"] . '%', "number" => $addressparts[0], "street[~]" => $addressparts[1] . "%"],
|
||||||
|
"LIMIT" => env("result_limit", 20),
|
||||||
|
"ORDER" => ["updated"]
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for ($i = 0; $i < count($results); $i++) {
|
||||||
|
$results[$i]["address"] = $results[$i]["number"] . " " . $results[$i]["street"];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove duplicates
|
||||||
|
$finalresults = [];
|
||||||
|
for ($i = 0; $i < count($results); $i++) {
|
||||||
|
$infinal = false;
|
||||||
|
for ($j = 0; $j < count($finalresults); $j++) {
|
||||||
|
if ($results[$i]["address"] == $finalresults[$j]["address"]) {
|
||||||
|
$infinal = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!$infinal) {
|
||||||
|
$finalresults[] = $results[$i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exit(json_encode(["status" => "OK", "results" => $finalresults]));
|
7
nbproject/private/private.properties
Normal file
7
nbproject/private/private.properties
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
copy.src.files=false
|
||||||
|
copy.src.on.open=false
|
||||||
|
copy.src.target=/var/www/PhpProject1
|
||||||
|
hostname=localhost
|
||||||
|
port=8000
|
||||||
|
run.as=INTERNAL
|
||||||
|
url=http://localhost:8000/
|
7
nbproject/project.properties
Normal file
7
nbproject/project.properties
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
include.path=${php.global.include.path}
|
||||||
|
php.version=PHP_74
|
||||||
|
source.encoding=UTF-8
|
||||||
|
src.dir=.
|
||||||
|
tags.asp=false
|
||||||
|
tags.short=false
|
||||||
|
web.root=.
|
9
nbproject/project.xml
Normal file
9
nbproject/project.xml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://www.netbeans.org/ns/project/1">
|
||||||
|
<type>org.netbeans.modules.php.project</type>
|
||||||
|
<configuration>
|
||||||
|
<data xmlns="http://www.netbeans.org/ns/php-project/1">
|
||||||
|
<name>AddressAutocompleteAPI</name>
|
||||||
|
</data>
|
||||||
|
</configuration>
|
||||||
|
</project>
|
Loading…
x
Reference in New Issue
Block a user