ananchev/php-api-creator

Scalable PHPApiCreator to make API creation easier

Installs: 11

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/ananchev/php-api-creator

v1.0.4 2024-09-25 21:30 UTC

This package is auto-updated.

Last update: 2025-12-26 16:46:52 UTC


README

PHPApiCreator is a PHP library designed to simplify the creation of scalable APIs. It provides a structured way to manage database connections, handle HTTP methods (GET, POST, PATCH, DELETE), and return consistent JSON responses.

Features

  • Easy Database Integration: Connect to a MySQL database effortlessly.
  • HTTP Method Handling: Supports GET, POST, PATCH, DELETE requests.
  • RESTful Design: Follows REST principles for API structure.
  • JSON Responses: Automatically sets headers and outputs in JSON.
  • Error Handling: Centralized and consistent error responses.

Installation

Install via Composer

composer require ananchev/php-api-creator

Or clone the repository

git clone https://github.com/ananchevegor/PHPApiCreator.git

Quick Start

Create an endpoint file (example: country.php) and add:

<?php

$currentEndpoint = "country";
$api = new PHPApiCreator($db_host, $db_name, $db_user, $db_pass);

$connection = $api->database_connection();

echo $api->SERVER_REQUEST($_SERVER, $token_code, $currentEndpoint, $connection);

How it works

Database Connection

$connection = $api->database_connection();

Routing

The router selects the correct handler based on the HTTP method:

  • GET → fetch data
  • POST → update an existing record
  • PATCH → partially update an existing record
  • DELETE → delete a record
echo $api->SERVER_REQUEST($_SERVER, $token_code, $currentEndpoint, $connection);

Dynamic Endpoints

$currentEndpoint defines the endpoint/table name:

$currentEndpoint = "country";

Error Handling

Unsupported request methods return a standardized JSON error response.

HTTP Methods

GET

$this->token($SERVER, $token_code)->GET($_GET, $connection, $currentEndpoint);

POST

$this->token($SERVER, $token_code)->POST($_POST, $connection, $currentEndpoint);

PATCH

$inputs = file_get_contents("php://input");
$this->token($SERVER, $token_code)->PATCH($inputs, $connection, $currentEndpoint);

DELETE

$inputs = file_get_contents("php://input");
$this->token($SERVER, $token_code)->DELETE($inputs, $connection, $currentEndpoint);

Configuration

Example config.php:

<?php
$host = "localhost";
$dbname = "your_database";
$user = "your_username";
$pass = "your_password";

Query Example

Endpoint for table country:

$currentEndpoint = "country";

Request

https://your-domain.com/webservices/country?where=name like 'Rus' and iso eq 'RU'

Response

{
  "table": "country",
  "time": 1726581512,
  "payload": [
    {
      "id": "177",
      "iso": "RU",
      "name": "RUSSIAN FEDERATION",
      "nicename": "Russian Federation",
      "iso3": "RUS",
      "numcode": "643",
      "phonecode": "70"
    }
  ]
}

Recommended Project Structure

project-root/
  webservices/
    country.php
    users.php
    orders.php
  config.php
  vendor/