现在我可以输出http://localhost/posts/
[
{
"id": "1",
"title": "first post",
"description": "here my first post",
"status": "Y"
},
{
"id": "2",
"title": "second post",
"description": "here my second post",
"status": "Y"
}
]
这里是我的源代码
if ($_SERVER['REQUEST_METHOD'] == "GET"){
@List($objnm, $objid) = explode('/', $_GET['url']);
if ($objnm == 'posts')
{
$post = $db->query('SELECT * FROM post WHERE status="Y"');
echo json_encode($post, JSON_PRETTY_PRINT);
}
else
{
http_response_code(401);
}
}
我的htaccess
RewriteEngine On
RewriteRule ^([^/]+)/? index.php?url=$1 [L,QSA]
我需要http://localhost/posts/1的输出 任何人都可以帮助我吗?
答案 0 :(得分:1)
我建议使用is_numeric()函数来检查第二个参数是否为数字。
if ($_SERVER['REQUEST_METHOD'] == "GET"){
@List($objnm, $objid) = explode('/', $_GET['url']);
if ($objnm == 'posts')
{
if(is_numeric($objid)) {
$post = $db->query('SELECT * FROM post WHERE status="Y" AND id="'.$objid.'";');
echo json_encode($post, JSON_PRETTY_PRINT);
}
else
{
$post = $db->query('SELECT * FROM post WHERE status="Y"');
echo json_encode($post, JSON_PRETTY_PRINT);
}
}
else
{
http_response_code(401);
}
}