Wamp服务器 - 无法执行任何PHP Rest API

时间:2016-03-19 22:13:32

标签: php mysql .htaccess rest wampserver

我在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]

我尝试执行POST&amp; Google Advanced REST Client中GET的{​​{1}},但我继续获得404。

响应:

categories

我知道为什么我只会收到此错误 Wamp正在端口81上运行。我可以很好地执行phpadmin。为什么这会导致问题,从几天开始就无法解决。

你能帮我解决一下这个错误。

任何帮助都非常感激。

非常感谢。

2 个答案:

答案 0 :(得分:1)

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

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

编辑:试试这个htaccess文件。

null

答案 1 :(得分:1)

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

如果不是,您的.htaccess基本上会被忽略。最好在打击应该正常工作的htaccess语法之前检查一下。希望它有所帮助!