<?php header('Content-type: text/html; charset=UTF-8'); if (count($_REQUEST)>0){ require_once 'apiEngine.php'; foreach ($_REQUEST as $apiFunctionName => $apiFunctionParams) { $APIEngine=new APIEngine($apiFunctionName,$apiFunctionParams); echo $APIEngine->callApiFunction(); break; } }else{ $jsonError->error='No function called'; echo json_encode($jsonError); } ?>
<?php require_once('MySQLiWorker.php'); require_once ('apiConstants.php'); class APIEngine { private $apiFunctionName; private $apiFunctionParams; // API API static function getApiEngineByName($apiName) { require_once 'apiBaseClass.php'; require_once $apiName . '.php'; $apiClass = new $apiName(); return $apiClass; } // //$apiFunctionName - API apitest_helloWorld //$apiFunctionParams - JSON function __construct($apiFunctionName, $apiFunctionParams) { $this->apiFunctionParams = stripcslashes($apiFunctionParams); // [0] - API, [1] - API $this->apiFunctionName = explode('_', $apiFunctionName); } // JSON function createDefaultJson() { $retObject = json_decode('{}'); $response = APIConstants::$RESPONSE; $retObject->$response = json_decode('{}'); return $retObject; } // function callApiFunction() { $resultFunctionCall = $this->createDefaultJson();// JSON $apiName = strtolower($this->apiFunctionName[0]);// API if (file_exists($apiName . '.php')) { $apiClass = APIEngine::getApiEngineByName($apiName);// API $apiReflection = new ReflectionClass($apiName);// try { $functionName = $this->apiFunctionName[1];// $apiReflection->getMethod($functionName);// $response = APIConstants::$RESPONSE; $jsonParams = json_decode($this->apiFunctionParams);// JSON if ($jsonParams) { if (isset($jsonParams->responseBinary)){// JSON, zip, png . return $apiClass->$functionName($jsonParams);// API }else{ $resultFunctionCall->$response = $apiClass->$functionName($jsonParams);// API JSON } } else { // JSON $resultFunctionCall->errno = APIConstants::$ERROR_ENGINE_PARAMS; $resultFunctionCall->error = 'Error given params'; } } catch (Exception $ex) { // $resultFunctionCall->error = $ex->getMessage(); } } else { // API $resultFunctionCall->errno = APIConstants::$ERROR_ENGINE_PARAMS; $resultFunctionCall->error = 'File not found'; $resultFunctionCall->REQUEST = $_REQUEST; } return json_encode($resultFunctionCall); } } ?>
<?php class APIConstants { // - JSON public static $RESULT_CODE="resultCode"; // - JSON apiEngine public static $RESPONSE="response"; // public static $ERROR_NO_ERRORS = 0; // public static $ERROR_PARAMS = 1; // SQL public static $ERROR_STMP = 2; // public static $ERROR_RECORD_NOT_FOUND = 3; // . public static $ERROR_ENGINE_PARAMS = 100; // zip public static $ERROR_ENSO_ZIP_ARCHIVE = 1001; } ?>
<?php class MySQLiWorker { protected static $instance; // object instance public $dbName; public $dbHost; public $dbUser; public $dbPassword; public $connectLink = null; // new MySQLiWorker private function __construct() { /* ... */ } // private function __clone() { /* ... */ } // unserialize private function __wakeup() { /* ... */ } // public static function getInstance($dbName, $dbHost, $dbUser, $dbPassword) { if (is_null(self::$instance)) { self::$instance = new MySQLiWorker(); self::$instance->dbName = $dbName; self::$instance->dbHost = $dbHost; self::$instance->dbUser = $dbUser; self::$instance->dbPassword = $dbPassword; self::$instance->openConnection(); } return self::$instance; } // ->bind function prepareParams($params) { $retSTMTString = ''; foreach ($params as $value) { if (is_int($value) || is_double($value)) { $retSTMTString.='d'; } if (is_string($value)) { $retSTMTString.='s'; } } return $retSTMTString; } // public function openConnection() { if (is_null($this->connectLink)) { $this->connectLink = new mysqli($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName); $this->connectLink->query("SET NAMES utf8"); if (mysqli_connect_errno()) { printf(" : %s\n", mysqli_connect_error()); $this->connectLink = null; } else { mysqli_report(MYSQLI_REPORT_ERROR); } } return $this->connectLink; } // public function closeConnection() { if (!is_null($this->connectLink)) { $this->connectLink->close(); } } // public function stmt_bind_assoc(&$stmt, &$out) { $data = mysqli_stmt_result_metadata($stmt); $fields = array(); $out = array(); $fields[0] = $stmt; $count = 1; $currentTable = ''; while ($field = mysqli_fetch_field($data)) { if (strlen($currentTable) == 0) { $currentTable = $field->table; } $fields[$count] = &$out[$field->name]; $count++; } call_user_func_array('mysqli_stmt_bind_result', $fields); } } ?>
<?php class apiBaseClass { public $mySQLWorker=null;// // function __construct($dbName=null,$dbHost=null,$dbUser=null,$dbPassword=null) { if (isset($dbName)){// $this->mySQLWorker = MySQLiWorker::getInstance($dbName,$dbHost,$dbUser,$dbPassword); } } function __destruct() { if (isset($this->mySQLWorker)){ // , $this->mySQLWorker->closeConnection(); // } } // JSON function createDefaultJson() { $retObject = json_decode('{}'); return $retObject; } // JSON MySQLiWorker function fillJSON(&$jsonObject, &$stmt, &$mySQLWorker) { $row = array(); $mySQLWorker->stmt_bind_assoc($stmt, $row); while ($stmt->fetch()) { foreach ($row as $key => $value) { $key = strtolower($key); $jsonObject->$key = $value; } break; } return $jsonObject; } } ?>
<?php class apitest extends apiBaseClass { //http://www.example.com/api/?apitest.helloAPI={} function helloAPI() { $retJSON = $this->createDefaultJson(); $retJSON->withoutParams = 'It\'s method called without parameters'; return $retJSON; } //http://www.example.com/api/?apitest.helloAPIWithParams={"TestParamOne":"Text of first parameter"} function helloAPIWithParams($apiMethodParams) { $retJSON = $this->createDefaultJson(); if (isset($apiMethodParams->TestParamOne)){ // , $retJSON->retParameter=$apiMethodParams->TestParamOne; }else{ $retJSON->errorno= APIConstants::$ERROR_PARAMS; } return $retJSON; } //http://www.example.com/api/?apitest.helloAPIResponseBinary={"responseBinary":1} function helloAPIResponseBinary($apiMethodParams){ header('Content-type: image/png'); echo file_get_contents("http://habrahabr.ru/i/error-404-monster.jpg"); } } ?>
function helloAPI() { $retJSON = $this->createDefaultJson(); $retJSON->withoutParams = 'It\'s method called without parameters'; return $retJSON; }
function helloAPIWithParams($apiMethodParams) { $retJSON = $this->createDefaultJson(); if (isset($apiMethodParams->TestParamOne)){ // , $retJSON->retParameter=$apiMethodParams->TestParamOne; }else{ $retJSON->errorno= APIConstants::$ERROR_PARAMS; } return $retJSON; }
function helloAPIResponseBinary($apiMethodParams){ header('Content-type: image/jpeg'); echo file_get_contents("http://habrahabr.ru/i/error-404-monster.jpg"); }
Source: https://habr.com/ru/post/143317/
All Articles