PHP code example of hrishikesh214 / php-api

1. Go to this page and download the library: Download hrishikesh214/php-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hrishikesh214 / php-api example snippets




$client = new phpapi\Client();

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

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

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

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

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

$client->trace(true|false);

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

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

//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'
];
Easily create RESTFull API in PHP
apacheconf
composer