PHPackages                             hrishikesh214/php-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. hrishikesh214/php-api

ActiveLibrary[API Development](/categories/api)

hrishikesh214/php-api
=====================

Helps easily build RESTFul Api in PHP

v1.7(5y ago)125GPL-3.0-or-laterPHP

Since Feb 26Pushed 5y ago1 watchersCompare

[ Source](https://github.com/hrishikesh214/php-api)[ Packagist](https://packagist.org/packages/hrishikesh214/php-api)[ RSS](/packages/hrishikesh214-php-api/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)DependenciesVersions (9)Used By (0)

[`PHP` API](https://github.com/hrishikesh214/php-api)
=====================================================

[](#php-api)

### `Easily create RESTFull API in PHP`

[](#easily-create-restfull-api-in-php)

```
It supports only JSON but you can change output :-)
```

Index
=====

[](#index)

1. [Installation](#installation)
2. [Documentation](#documentation)
3. [Creating Endpoint](#creating-an-endpoint)
4. [Passing URL Parameters](#passing-url-Parameters)
5. [Post Parameters](#pOST-endpoint)
6. [API Tracer](#trace-whole-api)
7. [Setting Error Handlers](#setting-request-errors)
8. [External Routes](#external-routes)

Installation
============

[](#installation)

```
composer require hrishikesh214/php-api
```

And then in your file

```
require 'vendor/autoload.php';
```

Configurations
==============

[](#configurations)

Before getting into `PHPAPI` we have to make some configs

In your root directory, create `.htaccess` file and paste following code

```
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?URL=$1 [L]
```

Documentation
=============

[](#documentation)

### `Creating a client`

[](#creating-a-client)

```
$client = new phpapi\Client();
```

You can also pass `base address`

```
$client = new phpapi\Client("api/");
```

All child endpoints will be access after `api/`

`Running Client`
----------------

[](#running-client)

```
print_r($client->run(isset($_GET['URL']) ? $_GET['URL'] : ""));
```

This `$client->run()` returns response string, so you can store it in variable for further process or directly print it as response

### `Creating an Endpoint`

[](#creating-an-endpoint)

```
$client->mount('GET', 'token', function(){
    //some calculations
    return $token;
});
```

Only some request methods are allowed : `'PUT', 'POST', 'DELETE', 'PATCH', 'GET', 'PURGE'`

`mount` returns boolean value about whether endpoint is mounted successfully or not

### `Passing URL Parameters`

[](#passing-url-parameters)

`Client` will automatically pass url parameters to the response function

example is given below

```
$client->mount('GET', 'wish/:name/:age', function($props){
    return "Hi $props['name'], you are $props['age] years old!";
});
```

URI Parameters are directly passed to the responder function in an associative array according to the key passed in parameter

All other `posted` parameters will automatically get stored in `$_POST`

`NOTE` If parameter is define while mounting but not passes then it is given a null value

### `POST Endpoint`

[](#post-endpoint)

```
$respond = function(){
    return $_POST; //It will have all posted data!
};
$client->mount("POST", 'checkpost', $respond);
```

### Trace Whole API

[](#trace-whole-api)

You can trace whole API Client with detailed configs of all endpoints mounted with a function

```
$client->trace(true|false);
```

If Above value is true final api result will also included api configs

***This Also includes Requested configs***

Example output:-

```
{
  "track": {
    "routes": {
      "POST": {
        "api/wish": {
          "base": "api/wish",
          "type": "POST",
          "params": [ ]
        },
        "name": {
          "base": "name",
          "type": "POST",
          "params": [ ]
        }
      },
      "GET": {
        "api/msg": {
          "base": "api/msg",
          "type": "GET",
          "params": [ ]
        },
        "api/wish": {
          "base": "api/wish",
          "type": "GET",
          "params": {
            "name": 3
          }
        },
        "name": {
          "base": "name",
          "type": "GET",
          "params": {
            "name": 3
          }
        },
        "/": {
          "base": "/",
          "type": "GET",
          "params": [ ]
        }
      }
    },
    "base": "",
    "request_blocks": [
      "api",
      "msg"
    ],
    "request_uri": "api/msg",
    "request_type": "GET"
  },
  "result": "trial"
}
```

In above example result will contain result coming from API

Setting Request Errors
----------------------

[](#setting-request-errors)

There is a default error handler but you change it!

### 404

[](#404)

```
$client->set404([
    'error_type' => 404,
    'error_msg' => "Not Found"
]);
```

### 405

[](#405)

```
$client->set405([
    'error_type' => 405,
    'error_msg' => "Method Not Allowed"
]);
```

External Routes
---------------

[](#external-routes)

### `Importing Routes from external file`

[](#importing-routes-from-external-file)

You can also define routes in external file all you need is to use `Helper` Class.

For example (folder structure):-

```
|   myRoutes
|       - api.php
|   vendor (composer files)
|   index.php

```

```
//index.php
$client = new phpapi\Client();
$helper = new phpapi\Helper($client);

$helper->use('myRoutes/api.php' [, basename: 'api']);
```

```
// myRoutes/api.php

// You can define functions and also pass to callback
$myFunc = function($props){
            return "Good morning {$props['name']}";
        };

$routes = [
    [
        "match" => 'msg',
        "type" => "get",
        "callback" => function(){
            return "trial";
        }
    ],
    [
        "match" => 'wish/:name',
        "type" => "get",
        "callback" => $myFunc
    ],
    [
        "match" => 'wish',
        "type" => "post",
        "callback" => function(){
            return "Good morning {$_POST['name']}";
        }
    ]
];

$config = [
    'base' => 'api'
];
```

In above code `$config['base']'` will be act as base to all routes present in this file.

`$routes` will contain all routes.

*Please maintain format else code will not work!*

### Made with ❤️By [Hrishikesh](https://github.com/hrishikesh214)

[](#made-with-️by-hrishikesh)

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

8

Last Release

1906d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2f12410a6e357207f3725664e97daa18bef9f4caac667dff23472dd12af7fb4c?d=identicon)[hrishikesh214](/maintainers/hrishikesh214)

---

Top Contributors

[![hrishikesh214](https://avatars.githubusercontent.com/u/61156101?v=4)](https://github.com/hrishikesh214 "hrishikesh214 (27 commits)")

### Embed Badge

![Health badge](/badges/hrishikesh214-php-api/health.svg)

```
[![Health](https://phpackages.com/badges/hrishikesh214-php-api/health.svg)](https://phpackages.com/packages/hrishikesh214-php-api)
```

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M480](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[facebook/php-business-sdk

PHP SDK for Facebook Business

90821.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

74513.7M114](/packages/meilisearch-meilisearch-php)[google/gax

Google API Core for PHP

265103.1M454](/packages/google-gax)[google/common-protos

Google API Common Protos for PHP

173103.7M50](/packages/google-common-protos)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
