我目前正在致力于在纯 oop php 中构建一个测试“api”。 一切工作正常,但现在当我尝试调用“/api.php::getfinantatori”的 GET 时,出现 403 禁止错误。 // api.php 代码
<?php
require_once 'autoloader.php';
require_once 'classes/finantator.php';
// Handle API requests
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$requestUri = $_SERVER['REQUEST_URI'];
$apiEndpoint = '/api.php::';
$commandPos = strpos($requestUri, $apiEndpoint) + strlen($apiEndpoint);
if ($commandPos !== false) {
$commandWithParams = substr($requestUri, $commandPos);
list($command, $queryParams) = explode('?', $commandWithParams);
parse_str($queryParams, $params);
$finantatorAPI = new FinantatorAPI();
switch ($command) {
case 'getfinantatori':
$finantatori = $finantatorAPI->getFinantatori();
echo json_encode($finantatori);
break;
case 'getfinantatoribyparam':
$filterFinantatorId = isset($params['finantatorId']) ? $params['finantatorId'] : null;
$filterAbreviere = isset($params['abreviere']) ? $params['abreviere'] : null;
$filterDescriere = isset($params['descriere']) ? $params['descriere'] : null;
$filteredFinantatori = $finantatorAPI->getFinantatoriByFilter($filterFinantatorId, $filterAbreviere, $filterDescriere);
echo json_encode($filteredFinantatori);
break;
default:
http_response_code(400); // Bad Request
echo json_encode(array('error' => 'Invalid command'));
}
}
}
//finantatori.php 代码
<?php
class Finantator {
public $finantatorId;
public $abreviere;
public $descriere;
function __construct($finantatorId, $abreviere, $descriere) {
$this->finantatorId = $finantatorId;
$this->abreviere = $abreviere;
$this->descriere = $descriere;
}
}
class FinantatorAPI {
private $data; // Array to hold the objects
function __construct() {
$this->data = $this->readDataFromCSV(); // Read data from CSV and populate the array
}
private function readDataFromCSV() {
$data = array();
// Replace 'path/to/your/csv/file.csv' with the actual path to your CSV file
$csvFile = fopen('F:\xampp\htdocs\model\finantatori.csv', 'r');
if ($csvFile !== false) {
while (($dataRow = fgetcsv($csvFile)) !== false) {
// Assuming the CSV columns are in the order: finantatorId, abreviere, descriere
$finantatorId = $dataRow[0];
$abreviere = $dataRow[1];
$descriere = $dataRow[2];
$data[] = new Finantator($finantatorId, $abreviere, $descriere);
}
fclose($csvFile);
}
return $data;
}
public function getFinantatori() {
return $this->data;
}
public function getFinantatoriByFilter($filterFinantatorId = null, $filterAbreviere = null, $filterDescriere = null) {
$filteredData = array();
foreach ($this->data as $finantator) {
if (($filterFinantatorId === null || $this->isWildcardMatch($filterFinantatorId, $finantator->finantatorId)) &&
($filterAbreviere === null || $this->isWildcardMatch($filterAbreviere, $finantator->abreviere)) &&
($filterDescriere === null || $this->isWildcardMatch($filterDescriere, $finantator->descriere))
) {
$filteredData[] = $finantator;
}
}
return $filteredData;
}
private function isWildcardMatch($filterValue, $actualValue) {
return fnmatch($filterValue, $actualValue, FNM_CASEFOLD);
}
}
我尝试解决 .htacess 文件添加“ RewriteEngine On RewriteRule ^api.php-(.*)$ api.php?$1 [QSA,L] " 其中但没有修复。