Single file PHP script that adds a REST API to a SQL database
Single file PHP script that adds a REST API to a SQL database
Single file PHP script that adds a REST API to a MySQL/MariaDB, PostgreSQL, SQL Server or SQLite database.
Howto: Upload "api.php" to your webserver, configure it to connect to your
database, have an instant full-featured REST API.
NB: This is the TreeQL reference implementation in PHP.
Download the "api.php" file from the latest release:
https://github.com/mevdschee/php-crud-api/releases/latest or direct from:
https://raw.githubusercontent.com/mevdschee/php-crud-api/main/api.php
This is a single file application! Upload "api.php" somewhere and enjoy!
For local development you may run PHP's built-in web server:
php -S localhost:8080Test the script by opening the following URL:
http://localhost:8080/api.php/records/posts/1Don't forget to modify the configuration at the bottom of the file.
Alternatively you can integrate this project into the web framework of your choice, see:
In these integrations Composer is used to load this project as a dependency.
For people that don't use composer, the file "api.include.php" is provided.
This file contains everything from "api.php" except the configuration from
"src/index.php" and can be used by PHP's "include" function.
Edit the following lines in the bottom of the file "api.php":
$config = new Config([
'username' => 'xxx',
'password' => 'xxx',
'database' => 'xxx',
]);These are all the configuration options and their default value between brackets:
mysql, pgsql, sqlsrv or sqlite (mysql)localhost)4326)cors)records,geojson,openapi,status){"info":{"title":"PHP-CRUD-API","version":"1.0.0"}})3)0)TempFile, Redis, Memcache, Memcached or NoCache
(TempFile)10)JSON_UNESCAPED_UNICODE)false)All configuration options are also available as environment variables. Write the config option with capitals, a "PHP_CRUD_API_" prefix and underscores for word breakes, so for instance:
The environment variables take precedence over the PHP configuration.
These limitation and constrains apply:
The following features are supported:
Related projects:
There are also proof-of-concept ports of this script that only support basic REST CRUD functionality in: PHP, Java, Go, C# .net core, Node.js and Python.
You can install all dependencies of this project using the following command:
php install.phpYou can compile all files into a single "api.php" file using:
php build.phpNote that you don't use compilation when you integrate this project into another project or framework (use Composer instead).
You can access the non-compiled code at the URL:
http://localhost:8080/src/records/posts/1The non-compiled code resides in the "src" and "vendor" directories. The
"vendor" directory contains the dependencies.
You can update all dependencies of this project using the following command:
php update.phpThis script will install and run Composer to update the dependencies.
NB: The update script will patch the dependencies in the vendor directory for PHP 7.0 compatibility.
The scripts that publish a new version and the docker image are in the "scripts" directory, see "scripts/README.md" for how to use them.
TreeQL allows you to create a "tree" of JSON objects based on your SQL database structure (relations) and your query.
It is loosely based on the REST standard and also inspired by json:api.
The example posts table has only a a few fields:
posts
=======
id
title
content
createdThe CRUD + List operations below act on this table.
If you want to create a record the request can be written in URL format as:
POST /records/postsYou have to send a body containing:
{
"title": "Black is the new red",
"content": "This is the second post.",
"created": "2018-03-06T21:34:01Z"
}And it will return the value of the primary key of the newly created record:
2To read a record from this table the request can be written in URL format as:
GET /records/posts/1Where "1" is the value of the primary key of the record that you want to read. It will return:
{
"id": 1
"title": "Hello world!",
"content": "Welcome to the first post.",
"created": "2018-03-05T20:12:56Z"
}On read operations you may apply joins.
To update a record in this table the request can be written in URL format as:
PUT /records/posts/1Where "1" is the value of the primary key of the record that you want to update. Send as a body:
{
"title": "Adjusted title!"
}This adjusts the title of the post. And the return value is the number of rows that are set:
1If you want to delete a record from this table the request can be written in URL format as:
DELETE /records/posts/1And it will return the number of deleted rows:
1To list records from this table the request can be written in URL format as:
GET /records/postsIt will return:
{
"records":[
{
"id": 1,No open issues yet, or sync has not completed.