/FSAPI

Simple API JSON

Primary LanguagePHPGNU General Public License v3.0GPL-3.0

FSAPI - Simple JSON API

About

This project use a source file "DEINFO_AB_FEIRASLIVRES_2014.csv" at "http://www.prefeitura.sp.gov.br/cidade/secretarias/upload/chamadas/feiras_livres_1429113213.zip".

The project's objective is learning about JSON API.

Firefox Plugin (restclient.net) was used to develop.

Requirements

APACHE (or other), PHP5, MySQL. Also called LAMP.

First steps

Place the code in your webserver '/var/www/html'.

Enter in prompt shell and type:
mysql -uUser -p [ENTER] type your user and password.
Open database.sql, copy the 25 lines, and paste in your mysql prompt command.
Database name: feiras
Table name: cadastrofeiras

Open the file api.php and change it according to your mysql database settings.

$hostname = "localhost"
$username = "root"
$password = "root"

Important: the files should be inside the FSAPI folder.


ERRORS
200 OK Everthing is alright.
202 ACCEPTED Your query was succeed but not returning any information.
400 BAD REQUEST Syntax error (example: get parameter without any complete URL http://localhost/FSAPI/api.php/v1, or when you try to create another register in the database and it already exists).
405 METHOD NOT ALLOWED Method or parameter not implemented (example http://localhost/FSAPI/api.php/v1/new)

REQUEST
http://localhost/FSAPI/api.php/v1/listarfeiras -> List all rows from table.
http://localhost/FSAPI/api.php/v1/listarfeiras?limit=10 -> List 10 rows from table.
Notice that the return fields are numerics 0-16 or by table's name. 0 equals 'ID', 1 equals "LONGITUDE" ... 16 equals "REFERENCIA".
    [
    {
       "0": "1",
       "1": "-46550164",
       "2": "-23558733",
       "3": "2147483647",
       "4": "2147483647",
       "5": "87",
       "6": "VILA FORMOSA",
       "7": "26",
       "8": "ARICANDUVA-FORMOSA-CARRAO",
       "9": "Leste",
       "10": "Leste 1",
       "11": "VILA FORMOSA",
       "12": "4041-0",
       "13": "RUA MARAGOJIPE",
       "14": "S/N",
       "15": "VL FORMOSA",
       "16": "TV RUA PRETORIA ",
       "ID": "1",
       "LONGITUDE": "-46550164",
       "LATITUDE": "-23558733",
       "SETCENS": "2147483647",
       "AREAP": "2147483647",
       "CODDIST": "87",
       "DISTRITO": "VILA FORMOSA",
       "CODSUBPREF": "26",
       "SUBPREFE": "ARICANDUVA-FORMOSA-CARRAO",
       "REGIAO5": "Leste",
       "REGIAO8": "Leste 1",
       "NOME_FEIRA": "VILA FORMOSA",
       "REGISTRO": "4041-0",
       "LOGRADOURO": "RUA MARAGOJIPE",
       "NUMERO": "S/N",
       "BAIRRO": "VL FORMOSA",
       "REFERENCIA": "TV RUA PRETORIA "
   },
       {...},
       {...},
       ...
    ]

http://localhost/FSAPI/api.php/v1/buscar/distrito/YOUR SEARCH -> Search in field distrito.
http://localhost/FSAPI/api.php/v1/buscar/distrito?limit=2/YOUR SEARCH -> Search in field distrito limited by 2 results.
http://localhost/FSAPI/api.php/v1/buscar/nomefeira/YOUR SEARCH -> Search in field nome_feira from table.
http://localhost/FSAPI/api.php/v1/buscar/nomefeira?limit=5/YOUR SEARCH -> Search in field nome_feira from table limited by 5 rows.


POST

ID can be null. All fields are required.

http://localhost/FSAPI/api.php/v1/cadastrarfeira
   [{    "ID": "1",
       "LONGITUDE": "-46550164",
       "LATITUDE": "-23558733",
       "SETCENS": "2147483647",
       "AREAP": "2147483647",
       "CODDIST": "87",
       "DISTRITO": "VILA FORMOSA",
       "CODSUBPREF": "26",
       "SUBPREFE": "ARICANDUVA-FORMOSA-CARRAO",
       "REGIAO5": "Leste",
       "REGIAO8": "Leste 1",
       "NOME_FEIRA": "VILA FORMOSA",
       "REGISTRO": "4041-0",
       "LOGRADOURO": "RUA MARAGOJIPE",
       "NUMERO": "S/N",
       "BAIRRO": "VL FORMOSA",
       "REFERENCIA": "TV RUA PRETORIA "
   }]

DELETE

ID required.

http://localhost/FSAPI/api.php/v1/excluirfeiras
[{ "ID": "1" }]
or
[{ "0": "879" }]

PUT

ID required.

http://localhost/FSAPI/api.php/v1/editarfeiras
[
  {    "ID": "1",
       "NUMERO": "S/N",
       "BAIRRO": "VL FORMOSA",
       "REFERENCIA": "TEST CHANGE"
   }
 ]