承接 ananchev/php-api-creator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ananchev/php-api-creator

最新稳定版本:v1.0.4

Composer 安装命令:

composer require ananchev/php-api-creator

包简介

Scalable PHPApiCreator to make API creation easier

README 文档

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/

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2024-09-17