[英]Getting 404 Page Not Found when accessing my index.php file
我一直在开发Android应用程序,现在我想在服务器上拥有一个数据库,我的应用程序将从该数据库中获取数据。
我发现用PHP制作REST API来访问MySQL数据库是最简单的解决方案,但是与php完全无关(第一次接触它)。
尝试点击时出现“ 404页面未找到”信息-http:// localhost / oneview_alerts / v1 / notifications
我的WAMP服务器已启动并正在运行。
我的代码很小很简单,我引用了以下URL来寻求帮助:1) https://www.androidhive.info/2014/01/how-to-create-rest-api-for-android-app-using -php纤薄和MySQL的日-12-2 /
我对PHP绝对陌生,无法理解从哪里开始调试。 它不像我的Java或Python,我可以立即在logcat或调试器中看到错误日志。 也许有办法,我只是不知道。
请帮忙!!
在我的access.log文件中获得的是“ 127.0.0.1--[26 / Dec / 2018:19:15:59 +0000]” GET / oneview_alerts / v1 / notifications HTTP / 1.1“ 404532”
的index.php
<?php
/**
* Created by PhpStorm.
* User: JM00490784
* Date: 12/26/2018
* Time: 6:05 PM
*/
ini_set('display_errors', 'On');
error_reporting(E_ALL);
require_once '../include/DbHandler.php';
require '.././libs/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
function echoResponse($status_code, $response) {
$app = \Slim\Slim::getInstance();
// Http response code
$app->status($status_code);
// setting response content type to json
$app->contentType('application/json');
echo json_encode($response);
}
$app->run();
$app->get('/notifications', function() {
$response = array();
$db = new DbHandler();
// fetching all user tasks
$result = $db->getAllNotifications();
$response["error"] = false;
$response["notifications"] = array();
// looping through result and preparing tasks array
while ($notification = $result->fetch_assoc()) {
$tmp = array();
$tmp["id"] = $notification["id"];
$tmp["notification_text"] = $notification["notification_text"];
$tmp["hostname"] = $notification["hostname"];
$tmp["service"] = $notification["service"];
$tmp["value"] = $notification["value"];
$tmp["timestamp"] = $notification["timestamp"];
array_push($response["notifications"], $tmp);
}
echoResponse(200, $response);
});
?>
DBHandler.php
<?php
/**
* Class to handle db operations
* This class will have CRUD methods for database tables
*
* @author Jotinder Singh Matta
*/
class DbHandler {
private $conn;
function __construct() {
require_once dirname(__FILE__) . './DbConnect.php';
// opening db connection
$db = new DbConnect();
$this->conn = $db->connect();
}
public function getAllNotifications() {
$stmt = $this->conn->prepare("SELECT * FROM notification ORDER BY timestamp DESC");
$stmt->execute();
$notifications = $stmt->get_result();
$stmt->close();
return $notifications;
}
}
?>
DBConnect.php
<?php
/**
* Handling database connection
*
* @author Jotinder Singh Matta
*/
class DbConnect {
private $conn;
function __construct() {
}
/**
* Establishing database connection
* @return database connection handler
*/
function connect() {
include_once dirname(__FILE__) . './Config.php';
// Connecting to mysql database
$this->conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check for database connection error
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// returing connection resource
return $this->conn;
}
}
?>
config.php文件
<?php
/**
* Database configuration
*/
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');
define('DB_NAME', 'oneview_alerts');
?>
的.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
通知表
desc notification
id int(11) NO PRI
NULL
auto_increment
notification_text varchar(250) YES
NULL
hostname varchar(255) NO
NULL
service varchar(255) NO
NULL
value varchar(255) NO
NULL
timestamp timestamp YES CURRENT_TIMESTAMP
根据您的评论,我假设您的文档根目录为E:\\wamp64\\www\\oneview_alerts\\v1\\
并且存在一个与index.php处于同一级别的.htaccess
文件。
以此替换.htaccess
文件的内容。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
然后创建一个虚拟主机文件
<VirtualHost *:80>
DocumentRoot "E:/wamp64/www/oneview_alerts/v1/"
ServerName oneview_alerts.local
<Directory "E:/wamp64/www/oneview_alerts/v1/">
</Directory>
</VirtualHost>
将以下内容添加到您的主机文件
127.0.0.1 localhost oneview_alerts.local
然后重新启动WAMP服务器
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.