Recommend this page to a friend! |
Classes of Marco Cesarato | PHP API Database | README.md | Download |
|
DownloadPHP Database Web API![](cover.png) Version: 0.7.0 Github: https://github.com/marcocesarato/Database-Web-API Author: Marco Cesarato DescriptionDynamically generate RESTful APIs from the contents of a database table. Provides JSON, XML, and HTML. Supports most popular databases. What problem this solvesCreating an API to access information within existing database tables is laborious task, when done as a bespoke task. This is often dealt with by exporting the contents of the database as CSV files, and providing downloads of them as a ?good enough? solution. How this solves itDatabase Web API acts as a filter, sitting between a database and the browser, allowing users to interact with that database as if it was a native API. The column names function as the key names. This obviates the need for custom code for each database layer. When Alternative PHP Cache (APC) is installed, parsed data is stored within APC, which accelerates its functionality substantially. While APC is not required, it is recommended highly. WikiGo to this link for go to the wiki of the platform and have a better informations about the usage of this platform and have more examples: https://github.com/marcocesarato/Database-Web-API/wiki Databases supported
Requirements
Installation
ConfigurationEdit Define API Name
Define datasetsDataset configurations: | Settings | Description | Default |
|------------------|------------------------------------------------------------------------------------|-----------|
| default | Default dataset | false |
| api | Accessible through API | true |
| name | Database Name | |
| username | Database Username | root |
| password | Database Password | root |
| server | Database Server Address | localhost |
| ttl | Cache time to live (set 1 for disable) | 3600 |
| port | Database Port | 3306 |
| type | Database Type (ex. Example complete with explanation
Note: All fields of ExampleHere is a dataset example for a MySQL database named ?inspections,? accessed with a MySQL user named ?website? and a password of ?s3cr3tpa55w0rd,? with MySQL running on the same server as the website, with the standard port of 3306. All tables may be accessed by Database to API except for ?cache? and ?passwords,? and among the accessible tables, the ?password_hint? column may not be accessed via Database to API. All of this is registered to create an API named ?facility-inspections?.
Retrieving the contents of the table history within this dataset as JSON would be accomplished with a request for /facility-inspections/history.json. Note that it is the name of the dataset (facility-inspections) and not the name of the database (inspections) that is specified in the URL. For a SQLite database, simply provide the path to the database in name. For an Oracle database, you can either specify a service defined in tsnames.ora (e.g. dept_spending) or you can define an Oracle Instant Client connection string (e.g., //localhost:1521/dept_spending). _(Optional)_ Authentication systemAuth configuration| Setting | Description | Type |
|-----------------|-------------------------------------------------------------------|-------|
| sqlite | When enabled store token on SQLite file | Bool |
| sqlite_database | SQLite filename (only if sqlite = true) | Text |
| api_database | Set database name where create api_table (Only if sqlite = false) ( Users configuration| Setting | Description | Type |
|----------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------|
| database | Database where users are stored (
API StructureFormat availables:
Generic URL format for all kind of request:Standard
AuthenticationAuthentication needed for browse the database. The authentication permit to managed the privilege of the users (read, write, modify, delete)
Set the header Auth-Account with the username/user id and Auth-Password with the encrypted password like this: Request example:
Response example:
Example of token usage on GET, POST, PUT, PATCH and DELETE requests: Set the header Access-Token with the token values received from auth request like this:
Check AuthenticationCheck authentication check is needed for verify if a token is valid.
Request example:
Response example:
GET RequestRetrieve data from dataset
ex: Examples of GET requests:
Advanced searchNote: These examples are valid only for GET and PUT requests Search single value
Search multiple values
Specify column's table
Compare between two different table columns
Compare between different columns of main table
Additional parameters
ex: DocumentationPS: Work only with pgsql and mysql database type at the moment For get auto-documentation of a database table:
For have a separated file where document your database you can use POST RequestInsert data Single insert:
Multiple insert:
Note: At the moment you can add only one row for table Examples of POST requests: Single insert:
Multiple insert:
PATCH/PUT RequestUpdate data Single update:
Multiple update:
Note: At the moment you can update only one row for table Examples of PUT Requests: Single Update:
Multi-table Update:
Multiple Update:
DELETE RequestDelete data
Examples of DELETE Requests:
HooksFor write hooks you can use Remember to name file like this pattern: TipsYou can use this code for have a database instance and the current user authenticated row:
Hooks listhttps://github.com/marcocesarato/Database-Web-API/wiki/3.2)-Hooks:-List Most important hooks| Hook | Type | Description | Params | Return | |-----------------------|--------|-----------------------------------------------------------------|------------------------------------------------------------|--------| | sql_restriction | Filter | Add restriction on where conditions for each query | (string) $restriction (string) $table (string) $permission | String | | can_read | Filter | Return if can get/select | (bool) $permission = true | Bool | | can_write | Filter | Return if can post/insert | (bool) $permission = true | Bool | | can_edit | Filter | Return if can put/update | (bool) $permission = true | Bool | | can_delete | Filter | Return if can delete | (bool) $permission = true | Bool | | on_read | Filter | Result content returned on get/read | (array) $data (string) $table | Array | | on_write | Filter | Result content returned on post/write | (array) $data (string) $table | Array | | on_edit | Filter | Result content returned on put/edit | (array) $data (string) $table | Array | | on_delete | Filter | Get result content returned on delete | (array) $data (string) $table | Array | | render | Filter | Manipulate data response rendered | (array) $data (string) $query (string) $method | Array | Hooks detail
ClientsPHP ClientFilename: Class name: APIClient | Method | Description | Type | Parameters | Return | | ---------------- | -------------------------------------------------- | ----------------- | ---------------------------------- | ---------------------------------------------- | | getInstance | | public<br>static | | Returns static reference to the class instance | | isConnected | Is Connected | public | | bool | | setUrl | Set Url | public<br>static | string $url | | | setAccessToken | Set Access token | public<br>static | string $token | | | setDataset | Set Dataset | public<br>static | string $dataset | | | setTimeout | Set Timeout | public<br>static | int $timeout = 15 | | | setExecutionTime | Set max execution time | public<br>static | int $time = 60 | | | get | Get data | public | string $table<br>array $where | bool<br>mixed | | insert | Insert data | public | array $params | bool<br>mixed | | update | Update data | public | array $params | bool<br>mixed | | replace | Replace data | public | array $params | bool<br>mixed | | delete | Delete data | public | string $table<br>array $params | bool<br>mixed | | searchElement | Search object in array | public | $array<br>$key<br>$value | mixed | | filterBy | Filter object in array | public | $key<br>$value<br>$array<br>$limit | mixed | | filter | Filter object in array | public | $values<br>$array<br>$limit | mixed | Creditshttps://github.com/project-open-data/db-to-api <https://github.com/voku/php-hooks> |