[英]How to create rest API GET method in php using multiple filter
I have created a rest API GET method in php using a parameter id.我使用参数 id 在 php 中创建了一个 rest API GET 方法。 http://localhost:8012/REST-API/items/expenses?id=2.
http://localhost:8012/REST-API/items/expenses?id=2。 This is working fine but I want get data using http://localhost:8012/REST-API/items/expenses?id=2&year=2022.
这工作正常,但我想使用 http://localhost:8012/REST-API/items/expenses?id=2&year=2022 获取数据。 I am new to php and tried all possibilities but not getting proper response.
我是 php 的新手,尝试了所有的可能性,但没有得到正确的回应。 Here is the code below
这是下面的代码
<?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;
}
}
?>
<?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.")
);
}
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/([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]
As I said in the comment he last three rules are wrong:正如我在评论中所说,他最后三个规则是错误的:
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]
What happens in this url?在这个 url 中发生了什么?
http://example.com/expenses/2012
how do you detect that is an id
or year
? http://example.com/expenses/2012
你如何检测它是id
还是year
?
I suggest to use this scheme:我建议使用这个方案:
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
So the .htaccess
file will be: (just changed the last three rules)所以
.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]
Online tests:在线测试:
I have tested and it works in every case id/year
, id
and year
, bellow you can see that it passes for that url我已经测试过,它在每种情况下都有效
id/year
, id
和year
,下面你可以看到它通过了 url
Also you should change your php conditions你也应该改变你的 php 条件
if ($this->id && $this->year) {
//..
} else if ($this->id) {
//..
} else if ($this->year) {
//..
} else {
//..
}
Without this change it will never reach when id and year and setted如果没有这个改变,它永远不会到达 id 和 year 并设置
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.