Erstellen Sie eine REST-API-GET-Methode mit mehreren Filtern in PHP
P粉937769356
P粉937769356 2024-01-29 13:03:56
0
1
369

Ich habe eine REST-API-GET-Methode in PHP mit der Parameter-ID erstellt. http://localhost:8012/REST-API/items/expenses?id=2. Das funktioniert gut, aber ich möchte http://localhost:8012/REST-API/items/expenses?id=2&year=2022 verwenden, um die Daten abzurufen. Ich bin PHP-Neuling und habe alle Möglichkeiten ausprobiert, bekomme aber nicht die richtige Antwort. Unten ist der Code

Expenses.php in der

/class-Datei

<?php
class Expenses{   
    private $expensesTable = "expenses";      
    public $id;
    public $month;
    private $amount;
    public $year;
    
    public function __construct($db){
        $this->conn = $db;
    }   

    function expenses(){    
        if($this->year) {
            $stmt = $this->conn->prepare("SELECT * FROM ".$this->expensesTable." WHERE year = ?");
            $stmt->bind_param("i", $this->year);                    
        } 
        else if($this->id) {
            $stmt = $this->conn->prepare("SELECT * FROM ".$this->expensesTable." WHERE id = ?");
            $stmt->bind_param("i", $this->id);                  
        }
        else if($this->id && $this->year) {
            $stmt = $this->conn->prepare("SELECT * FROM ".$this->expensesTable." WHERE id = ? AND year= ?");
            $stmt->bind_param("i", $this->id,$this->year);                  
        }

        else {
            $stmt = $this->conn->prepare("SELECT * FROM ".$this->expensesTable);        
        }       
        $stmt->execute();           
        $result = $stmt->get_result();      
        return $result; 
    }
}
?>

readexpenses.php

<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

include_once '../config/Database.php';
include_once '../class/Expenses.php';

$database = new Database();
$db = $database->getConnection();
 
$expenses = new Expenses($db);

$expenses->id = (isset($_GET['id']) && $_GET['id']) ? $_GET['id'] : '0';
$expenses->year = (isset($_GET['year']) && $_GET['year']) ? $_GET['year'] : '0';
$expenses->extra = (isset($_GET['month']) && $_GET['month']) ? $_GET['month'] : '0';

$result = $expenses->expenses();

if($result->num_rows > 0){    
    $itemRecords=array();
    $itemRecords["expenses"]=array(); 
    while ($expense = $result->fetch_assoc()) {     
        extract($expense); 
        $itemDetails=array(
            "id" => $id,
            "month" => $month,
            "amount" => $amount,
            "year" => $year
        ); 
       array_push($itemRecords["expenses"], $itemDetails);
    }    
    http_response_code(200);     
    echo json_encode($itemRecords);
}else{     
    http_response_code(404);     
    echo json_encode(
        array("message" => "No item found.")
    );
}

.htaccess

RewriteEngine On    # Turn on the rewriting engine
RewriteRule ^read$ read.php [NC,L]
RewriteRule ^read/([0-9_-]*)$ read.php?id= [NC,L]
RewriteRule ^create$ create.php [NC,L]
RewriteRule ^update$ update.php [NC,L]
RewriteRule ^delete$ delete.php [NC,L]
RewriteRule ^expenses$ readexpenses.php [NC,L]
RewriteRule ^expenses/([0-9_-]*)$ readexpenses.php?year= [NC,L]
RewriteRule ^expenses/([0-9_-]*)$ readexpenses.php?id= [NC,L]
RewriteRule ^expenses/([0-9_-]*)$ readexpenses.php?id=&year= [NC,L]

P粉937769356
P粉937769356

Antworte allen(1)
P粉642919823

正如我在评论中所说,他最后三个规则是错误的:

RewriteRule ^expenses/([0-9_-]*)$ readexpenses.php?year=$1 [NC,L]
RewriteRule ^expenses/([0-9_-]*)$ readexpenses.php?id=$1 [NC,L]
RewriteRule ^expenses/([0-9_-]*)$ readexpenses.php?id=$1&year=$2 [NC,L]

这个网址会发生什么? http://example.com/expenses/2012 如何检测这是 idyear

我建议使用这个方案:

http://example.com/expenses/id/2012   -> readexpenses.php?id=2012
http://example.com/expenses/year/2022 -> readexpenses.php?year=2022
http://example.com/expenses/2012/2022 -> readexpenses.php?id=2012&year=2022

因此 .htaccess 文件将是:(刚刚更改了最后三个规则)

RewriteEngine On    # Turn on the rewriting engine
RewriteRule ^read$ read.php [NC,L]
RewriteRule ^read/([0-9_-]*)$ read.php?id=$1 [NC,L]
RewriteRule ^create$ create.php [NC,L]
RewriteRule ^update$ update.php [NC,L]
RewriteRule ^delete$ delete.php [NC,L]
RewriteRule ^expenses$ readexpenses.php [NC,L]
RewriteRule ^expenses/year/([0-9]+)$ readexpenses.php?year=$1 [NC,L]
RewriteRule ^expenses/id/([0-9]+)$ readexpenses.php?id=$1 [NC,L]
RewriteRule ^expenses/([0-9]+)/([0-9]+)$ readexpenses.php?id=$1&year=$2 [NC,L]

在线测试:

我已经测试过了,它在任何情况下都有效 id /年id年份 ,下面你可以看到它通过了该 url

您还应该更改您的 php 条件

if ($this->id && $this->year) {
  //..
} else if ($this->id) {
  //..
} else if ($this->year) {
  //..
} else {
  //..
}

如果没有此更改,它将永远不会达到 id 和年份设置的时间

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage