1. Go to this page and download the library: Download laswitchtech/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/ */
laswitchtech / php-api example snippets
//Import BaseModel class into the global namespace
use LaswitchTech\phpAPI\BaseModel;
class UserModel extends BaseModel {
public function getUsers($limit) {
return $this->select("SELECT * FROM users ORDER BY id ASC LIMIT ?", ["i", $limit]);
}
}
//Import BaseController class into the global namespace
use LaswitchTech\phpAPI\BaseController;
class UserController extends BaseController {
public function __construct($Auth){
// Set the controller Authentication Policy
$this->Public = true; // Set to false to construct($Auth);
}
public function listAction() {
try {
// Namespace: /user/list
// Check the request method
if($this->Method !== 'GET'){
throw new Error('Invalid request method.');
}
// Initialize the user model
$UserModel = new UserModel();
// Configure default limit
$Limit = 25;
// Check if the limit is set
if($this->getQueryStringParams('limit')){
$Limit = intval($this->getQueryStringParams('limit'));
}
// Get the users
$Users = $UserModel->getUsers($Limit);
// Check if the users were found
if(count($Users) <= 0){
throw new Error('Users not found.');
}
// Send the output
$this->output(
$Users,
array('Content-Type: application/json', 'HTTP/1.1 200 OK')
);
} catch (Error $e) {
// Set the error
$this->Error = $e->getMessage();
// Log the error
$this->Logger->error($e->getMessage());
// Send the output
$this->output(
array('error' => $this->Error . ' - Something went wrong! Please contact support.'),
array('Content-Type: application/json', 'HTTP/1.1 500 Internal Server Error'),
);
}
}
}
// Initiate Session
session_start();
// These must be at the top of your script, not inside a function
use LaswitchTech\phpAPI\phpAPI;
// Load Composer's autoloader
// Initiate Session
session_start();
// These must be at the top of your script, not inside a function
use LaswitchTech\phpLogger\phpLogger;
use LaswitchTech\phpSMS\phpSMS;
use LaswitchTech\SMTP\phpSMTP;
use LaswitchTech\phpDB\Database;
use LaswitchTech\phpAUTH\phpAUTH;
// Load Composer's autoloader
ur_auth_token')
->config('phone', 'your_twilio_phone_number');
// Initiate phpDB
$phpDB = new Database();
// Configure phpDB
$phpDB->config("host","localhost")
->config("username","demo")
->config("password","demo")
->config("database","demo3");
// Initiate phpSMTP
$phpSMTP = new phpSMTP();
// Configure phpSMTP
$phpSMTP->config("username","[email protected]")
->config("password","*******************")
->config("host","smtp.domain.com")
->config("port",465)
->config("encryption","ssl");
// Construct Hostnames
$Hostnames = ["localhost","::1","127.0.0.1"];
if(isset($_SERVER['SERVER_NAME']) && !in_array($_SERVER['SERVER_NAME'],$Hostnames)){
$Hostnames[] = $_SERVER['SERVER_NAME'];
}
if(isset($_SERVER['HTTP_HOST']) && !in_array($_SERVER['HTTP_HOST'],$Hostnames)){
$Hostnames[] = $_SERVER['HTTP_HOST'];
}
// Initiate phpAUTH
$phpAUTH = new phpAUTH();
// Configure phpAUTH
$phpAUTH->config("hostnames",$Hostnames)
->config("basic",false) // Enable/Disable Basic Authentication
->config("bearer",true) // Enable/Disable Bearer Token Authentication
->config("request",false) // Enable/Disable Request Authentication
->config("cookie",false) // Enable/Disable Cookie Authentication
->config("session",false) // Enable/Disable Session Authentication
->config("2fa",false) // Enable/Disable 2-Factor Authentication
->config("maxAttempts",5) // Max amount of authentication attempts per windowAttempts
->config("maxRequests",1000) // Max amount of API request per windowRequests
->config("lockoutDuration",1800) // 30 mins
->config("windowAttempts",100) // 100 seconds
->config("windowRequests",60) // 60 seconds
->config("window2FA",60) // 60 seconds
->config("windowVerification",2592000) // 30 Days
->init();
// Install phpAUTH
$Installer = $phpAUTH->install();
// Create a User
$User = $Installer->create("api",["username" => "[email protected]"]);
// Activate User
$User->activate();
// Verify User
$User->verify();
// Initiate phpConfigurator
$Configurator = new phpConfigurator('account');
// Save Account for Testing
$Configurator->set('account','url',"https://{$Hostname}/api.php")
->set('account','token',$User->get('username').":".$User->getToken());
sh
├── api.php
├── config
│ └── api.cfg
├── Controller
│ └── UserController.php
└── Model
└── UserModel.php
Model.php
Controller.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.