如何基于将其称为PHP

时间:2018-06-29 09:14:40

标签: php api

请使用一个小的API,但是出于这个问题的目的,让基本URL名称为api.example.com,并且我有4个API端点

api.example.com/place
api.example.com/delivery_detail
api.example.com/track
api.example.com/cancel

现在在 api.example.com/place

<?php 
require_once('functions.php');
$api = new Api;
$api->processApi();
?>

api.example.com/delivery_detail

   <?php 
require_once('functions.php');
$api = new Api;
$api->processApi();
?>

api.example.com/track

    <?php 
require_once('functions.php');
$api = new Api;
$api->processApi();
?>

api.example.com/cancel

       <?php 
require_once('functions.php');
$api = new Api;
$api->processApi();
?>

我有一个页面也称为 api.example.com/api.php ,它不是终点,而是一个generates JWT Token并保存一些公共函数参数即< / p>

   public function get_delivery_detail_delivery(){

   }

    public function track_osc_delivery(){

   }

      public function create_insert_new_delivery(){

   }

     public function cancel_osc_delivery(){

   }

现在我希望端点api.example.com/place仅能呼叫public function get_delivery_detail_delivery()

,还有端点         api.example.com/track只能呼叫public function track_osc_delivery()

,还有端点         api.example.com/place只能呼叫public function create_insert_new_delivery()

最终终点         api.example.com/cancel只能呼叫public function cancel_osc_delivery()

除了被调用的正确URL应该能够调用该公共功能之外,另一个URL应该不能调用本来不能调用的公共功能

constants.php

     <?php 


   /*security*/
   define('SECRETE_KEY', 'Loremipsumnottherealvalue');

   /*Data Type*/
   define('BOOLEAN', '1');
   define('INTEGER', '2');
   define('STRING',  '3');


   /*Error Codes*/
   define('REQUEST_METHOD_NOT_VALID',             100);
       ?>

rest.php

     require_once('constants.php');

  class Rest {
    protected $request;
    protected $serviceName;
    protected $param;
         public function processApi(){
        $api = new API;
        $rMethod = new reflectionMethod('API', $this->serviceName);
        if(!method_exists($api, $this->serviceName)){
           $this->throwError(API_DOST_NOT_EXIST, "API does not exist");
        }
        $rMethod->invoke($api);
    }
    }

functions.php

     <?php
   spl_autoload_register(function($className){
         $path = strtolower($className) . ".php";
         $patha = strtolower($className) . ".php";
         $pathaa = strtolower($className) . ".php";
         $pathaaa = strtolower($className) . ".php";
         $pathaaaa = strtolower($className) . ".php";
         $pathaaaaa = strtolower($className) . ".php";
         if ($patha == "dbconnect.php"){
            $path = "DbConnect.php"; 
         }

        if ($pathaa == "beforevalidexception.php"){
            $path = "BeforeValidException.php"; 
         }

          if ($pathaaa == "expiredexception.php"){
            $path = "ExpiredException.php"; 
         }


          if ($pathaaaa == "signatureinvalidexception.php"){
            $path = "SignatureInvalidException.php"; 
         }  

         if ($pathaaaaa == "customer.php" || $pathaaaaa == "customers.php"){
            $path = "customers.php"; 
         }           

         if(file_exists($path)){
             require_once($path);
         } else {
             echo "File $path is not found.";
             exit();
         }
   })


?>

请问我如何解决此问题。

0 个答案:

没有答案