AI prompts
base on 🐽 oink.php is a single-file PHP library to easily build APIs # 🐽 oink.php
oink.php is a free and open source API wrapper for PHP, in a single file.
## Installation
You can install it by downloading the `oink.php` file and including it in your project. It doesn't have dependencies and is compatible with PHP 8.0 or later.
You will need to make all requests go through your `index.php` file. This can be done by using an `.htaccess` file, a `web.config` file, or by configuring your web server. You can find examples of `.htaccess` files in the [examples](examples/) directory.
## Basic Usage
This minimal example shows how to create a simple blog API:
**index.php**
```php
require_once 'oink.php';
Oink\serve('endpoints.php');
```
**endpoints.php**
```php
require_once 'db.php'; // Your database functions
use function Oink\{str, enum, id, email};
function post_list() {
$page = id("page", default: 1, optional: true);
$limit = enum("limit", values: [10, 20, 50], default: 10, optional: true);
[$pages, $posts] = DB\posts($page, $limit);
return [
"page" => $page, "pages" => $pages, "limit" => $limit, "posts" => $posts
];
}
function comment_list() {
$post_id = id("post_id");
check(DB\post_exists($post_id), "postNotFound");
return ["comments" => DB\comments($post_id)];
}
function comment_create() {
$post_id = id("post_id");
$author = email("author");
$text = str("text", min: 5, max: 100);
check(DB\post_exists($post_id), "postNotFound");
return ["id" => DB\create_comment($post_id, $author, $text)];
}
```
Let's see what's happening here:
* **Routing:** When calling `serve`, it looks at all function names defined in the file passed as an argument and creates an endpoint for each one. Hence, going to `my.website/post/list` will call the `post_list` function. This routing can be customized by passing extra parameters to `serve`. The endpoints are method-agnostic, so you can use GET, POST, PUT, DELETE, etc., to access them.
* **Parameters:** In the endpoints file, the request parameters are read by calling functions with type names. For instance, calling `str("tag", optional: true)` will read the `tag` parameter, ensure it's a string, and return its value or `null` if not present. Parameters can come as form data, JSON, cookies, or even headers. By default, GET parameters are not allowed (since parameters are unaware of the request method, this could lead to CSRF attacks), but you can enable them by passing `allow_get: true` to `serve`.
* **Response:** The response is a JSON object or array returned by the endpoint function. If the function doesn't return anything, the response will be `{"success": true}`. If a parameter validation fails or if a `check` is evaluated to false, it will return a 400 error with `{"error": <reason>}`. If an exception is thrown, it will return a 500 error.
To know more about the usage, check out the [documentation](documentation.md) and the [examples](examples).
## Contributing
If you want to contribute, you can do so by forking the repository and creating a pull request. You should have a look at the file first to follow the same ethos and style. If you plan to add a new feature or extend existing functionality, please open an issue first to discuss it. Feel free to open an issue if you find a bug or have a suggestion.
> Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
>
> -- Antoine de Saint-Exupéry
## Why Oink?
Oink should feel like a pig in the mud: simple and comfortable, even though it's not the cleanest thing in the world. This library prioritizes development speed and simplicity over everything else, including best practices and standards. If you are looking for a highly customizable, modular, and scalable solution, you should look for a full-featured framework like Laravel, Symfony, or Lumen.
See more [here](https://jcarlosroldan.com/post/322/minimal-apis-with-oink-php).
", Assign "at most 3 tags" to the expected json: {"id":"7829","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"