Skip to content

Developing a smart API with a simple structure.

License

Notifications You must be signed in to change notification settings

Mehdi-RaTo/php-smart-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 

Repository files navigation

PHP Smart API

Developing a smart API with a simple structure.

1️⃣ Create

Create a file named Yow.php in the path /API/Services/ and place the following code in it.

<?php class Yow extends APIService
{
    // array of required params
    protected $requireParams = ["FirstName", "LastName"];

    function Run()
    {
        $firstName = $this->params["FirstName"];    // required param
        $midName = $this->params["MidName"] ?? "";  // optional param
        $lastName = $this->params["LastName"];      // required param

        $result = "Your name is $firstName $midName $lastName";

        return $result;
    }
}

➕ More examples in /API/Services/

2️⃣ Use

Fetch by JavaScript

fetch("https://example.com/API/GetService.php", {
    method: "POST",
    headers: {
        "Content-Type": "application/json"
    },
    body: JSON.stringify({
        "ServiceName": "Yow",
        "Parameters": {
            "FirstName": "Mehdi",
            "LastName": "RaTo"
        }
    })
}).then((response) => response.json()).then((response) => {
    console.log(response);
}).catch((error) => {
    console.log(error);
});

➕ Look at usage.html file for a better understanding.

💡 Request/Response Model

🔼 Request

{
    "ServiceName": "string",
    "Parameters": "object"
}

🔽 Response

{
    "IsSuccess": "boolean",
    "StatusCode": "integer",
    "Message": "string",
    "Result": "any"
}