【问题标题】:Wamp Server -- Cannot execute any PHP Rest APIWamp 服务器 - 无法执行任何 PHP Rest API
【发布时间】:2016-03-19 22:13:31
【问题描述】:

我在 Windows 64x 上安装了带有 Apache 2.4.9 的 Wamp Server 2.5。我在 c:\wamp\www\tridrops 文件夹下使用 Slim REST Api 项目创建了一个 PHP。我有我的 index.php,我试图在 c:\wamp\www\tridrops\tridrops\ver\index.php 执行。

我的 PHP 代码:

<?php

use Slim\Slim;
require_once '../include/DbHandler.php';
require_once '../libs/Slim-2.x/Slim/Slim.php';

\Slim\Slim::registerAutoloader();

$app = new Slim();

$app->run();

/***
 * Creating NEW CATEGORY in db  
 * method POST
 * params - category_name, category_description
 * url - /categories/
 */
$app->post('/categories', function() use ($app) {

// Check for required params
verifyRequiredParams(array('name', 'desc'));

$response = array();
// Reading post parameters
$category_name = $app->request()->post('name');
$category_description = $app->request()->post('desc');

$db = new DbHandler();

// Creating new Category
$categoryId = $db->createCategory($category_name, $category_description);

if ($categoryId != NULL) {
    $response["error"] = false;
    $response["message"] = "Category Created Successfully with Category ";
    $response["categoryId"] = $categoryId;
} else {
    $response["error"] = true;
    $response["message"] = "Failed to create Category. Please try again.";
}
echoRespnse(201, $response);

});

/**
 * Getting List of Categories
 * method GET
 * url /categories
 */
$app->get('/categories', function() {
$response = array();
$db = new DbHandler();

// fetching all categories
$result = $db->getAllCategories();

$response["error"] = false;
$response["categories"] = array();

// looping through results
while ($categories = $result->fetch_assoc()) {
    $tmp = array();
    $tmp["categoryId"] = $categories["categoryId"];
    $tmp["category_name"] = $categories["category_name"];
    $tmp["categoy_isparent"] = $categories["categoy_isparent"];
    $tmp["category_description"] = $categories["category_description"];

    array_push($response["categories"], $tmp);
}

echoRespnse(200, $response);
});

?>

DbHandler.PHP 文件

<?php

// DbHandler.php

/**
 * Class to handle DB operations
 * CRUD methods for database
 */

class DbHandler {
    private $conn;

function __construct() {
    require_once dirname(__FILE__) . './DbConnect.php';
    // Opening db connection
    $db = new DbConnect();
    $this->conn = $db->connect();               
}

/*------------------ category table methods ------------ */
/**
 * Creating new Category
 * @param String $category_name
 * @param String $category_description
 * @param boolean $category_isparent
 */
public function createCategory($category_name, $category_description) {
    $response = array();

    // First ccheck if category already exists
    if (! $this->isCategoryExists($category_name)) {
        // insert stmt
        $stmt = $this->conn->prepare("INSERT INTO category(category_name, category_description) values(?, ?)");
        $stmt->bind_param("ssss", $category_name, $category_description);

        $result = $stmt->execute();

        $stmt->close();

        // Check for successful insertion
        if ($result) {
            // Category Created Successfully
            return SUCCESSFUL;
        } else {
            // fAILED TO INSERT
            return FAILED;
        }           
    } else {
        // Same Category Exists
        return EXISTS;
    }

    return $response;
}

/**
 * Fetch all Categories
 */
public function getAllCategories() {
    $stmt = $this->conn->prepare("SELECT * FROM category");
    $stmt->execute();
    $cats = $stmt->get_result();
    $stmt.close();

    return $cats;
}

.htaccess

# tridrops/ver/.htaccess

RewriteEngine On 


# Always set these headers.
#Header always set Access-Control-Allow-Origin "*"
#Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
#Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

我尝试在 Google 高级 REST 客户端中执行 categories 中的 POSTGET,但我一直收到 404。

回应:

<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /tridrops/tridrops/ver/categories/ was not found on this server.</p>
<hr>
<address>Apache/2.4.9 (Win64) PHP/5.5.12 Server at localhost Port 81</address>
</body>

知道为什么我只会收到此错误。
Wamp 在 81 端口上运行。我可以很好地执行 phpadmin。为什么这会导致问题,几天以来无法弄清楚。

你能帮我解决这个错误吗?

任何帮助都非常感谢。

非常感谢。

【问题讨论】:

    标签: php mysql .htaccess rest wampserver


    【解决方案1】:

    看起来问题可能是重写问题。我看到你的重写规则在你的 htaccess 文件中被注释掉了。你想把所有东西都路由到 index.php 吗?

    您可能可以通过访问 ...index.php/categories 来测试这是否是问题所在,并且该页面应该加载。

    编辑:试试这个 htaccess 文件。

    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ index.php [QSA,L]
    

    【讨论】:

    • 感谢您的快速回复。我编辑了我的重写规则 - RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L] Restarted wamp & 并在 ARC & 浏览器中尝试了localhost:81/tridrops/tridrops/ver/categories。两者都给出相同的响应,即 404
    • 我没有在 httpd.conf 中的项目文件夹或任何文件中添加任何内容。有什么要求吗?我阅读了几个链接以在 \www 中添加每个文件夹的虚拟主机。这是指我的问题吗???
    • 我认为有两件事需要检查。确保 mod_rewrite 模块已打开。此外,重写规则正在寻找可能是 wamps 根文件夹的环境基础。尝试一个看起来像这样的 htaccess。 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^ index.php [QSA,L]
    • 我试过你提到的方法。但没有成功。我的 DB_HOST --define('DB_HOST', 'localhost:81');正如我所说,我的 wamp 在 81 端口上,所以我是这样写的。这是正确的吗?
    • 端口无关紧要。抱歉,我的格式不好。我将更新我的答案以包含一个新的 htaccess 文件来尝试。
    【解决方案2】:

    正如@Steve Parish 所说,您似乎没有设置/启用 URL 重写。虽然 Steve 正在帮助您使您的重写工作正常,但我还建议您检查重写工作所需的模块是否已启用。在此处尝试答案以检查是否启用了 mod_rewrite: How to check if mod_rewrite is enabled in php?

    如果不是,您的 .htaccess 基本上会被忽略。最好在与应该工作的 htaccess 语法作斗争之前检查这一点。希望对您有所帮助!

    【讨论】:

    • 感谢@Tiago,我检查了并且 mod_rewrite 可用。 Apache/2.4.9 (Win64) PHP/5.5.12 mod_rewrite 模块可用
    猜你喜欢
    • 2014-11-30
    • 2014-12-10
    • 2014-05-26
    • 2012-02-16
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    相关资源
    最近更新 更多