如何根据.htaccess
语法使用REST API显示所有数据?
.htaccess
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^read/([a-zA-Z_-]*)$ read.php?name=$1 [NC,L]
RewriteRule ^semua/?$ semua.php [NC,L]
呼叫read.php
来显示index.php
的某些数据时,会显示所有特定的电话品牌:
$name = $_POST['name'];
$url = "http://localhost/api/simpel/items/read/phonebrand";
$client = curl_init($url);
curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($client);
但是当从semua.php
呼叫index.php
时,邮递员什么也没显示:
$url = "http://localhost/api/simpel/items/semua";
$client = curl_init($url);
curl_setopt($client,CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($client);
允许显示所有数据的正确.htaccess
是什么?
read.php
:
<?php
include_once("../db_connect.php");
if(!empty($_GET['name'])) {
$name=$_GET['name'];
$items = getItems($name, $conn);
if(empty($items)) {
jsonResponse(200,"Items Not Found",NULL);
} else {
jsonResponse(200,"Item Found",$items);
}
} else {
jsonResponse(400,"Invalid Request",NULL);
}
function jsonResponse($status,$status_message,$data) {
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
header("Content-Type:application/json");
header("HTTP/1.1 ".$status_message);
$response['status']=$status;
$response['status_message']=$status_message;
$response['data']=$data;
$json_response = json_encode($response);
echo $json_response;
}
function getItems($name, $conn) {
$sql = "SELECT id, p.name, p.description, p.price, p.created FROM items p WHERE p.name LIKE '%".$name."%' ORDER BY p.created DESC";
$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
$data = array();
while( $rows = mysqli_fetch_assoc($resultset) ) {
$data[] = $rows;
}
return $data;
}
?>
semua.php
:
<?php
include_once("../db_connect.php");
$items = getItems($conn);
function jsonResponse($status,$status_message,$data) {
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
header("Content-Type:application/json");
header("HTTP/1.1 ".$status_message);
$response['status']=$status;
$response['status_message']=$status_message;
$response['data']=$data;
$json_response = json_encode($response);
echo $json_response;
}
function getItems($conn) {
$sql = "SELECT * FROM items p ORDER BY p.created DESC";
$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
$data = array();
while( $rows = mysqli_fetch_assoc($resultset) ) {
$data[] = $rows;
}
return $data;
}
?>
答案 0 :(得分:0)
尝试几个小时后找到了临时答案。
将.htaccess更改为:
BaseAttackType
和semua.php到:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^read/([a-zA-Z_-]*)$ read.php?name=$1 [NC,L]
RewriteRule ^semua/([a-zA-Z_-]*)$ semua.php?name=$1 [NC,L]
并使用:
从index.php调用<?php
include_once("../db_connect.php");
if(!empty($_GET['name'])) {
$name=$_GET['name'];
$items = getItems($name, $conn);
if(empty($items)) {
jsonResponse(200,"Items Not Found",NULL);
} else {
jsonResponse(200,"Item Found",$items);
}
} else {
jsonResponse(400,"Invalid Request",NULL);
}
function jsonResponse($status,$status_message,$data) {
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
header("Content-Type:application/json");
header("HTTP/1.1 ".$status_message);
$response['status']=$status;
$response['status_message']=$status_message;
$response['data']=$data;
$json_response = json_encode($response);
echo $json_response;
}
function getItems($name, $conn) {
$sql = "SELECT * FROM items p ORDER BY p.created DESC";
$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
$data = array();
while( $rows = mysqli_fetch_assoc($resultset) ) {
$data[] = $rows;
}
return $data;
}
?>