简体   繁体   English

Wamp Server —无法执行任何PHP Rest API

[英]Wamp Server — Cannot execute any PHP Rest API

I have Wamp Server 2.5 with Apache 2.4.9 on Windows 64x. 我在Windows 64x上具有带Apache 2.4.9的Wamp Server 2.5。 I created a PHP using Slim REST Api project under c:\\wamp\\www\\tridrops folder. 我使用Slim REST Api项目在c:\\ wamp \\ www \\ tridrops文件夹下创建了一个PHP。 I have my index.php, that I am trying to execute at c:\\wamp\\www\\tridrops\\tridrops\\ver\\index.php . 我有我的index.php,正在尝试在c:\\ wamp \\ www \\ tridrops \\ tridrops \\ ver \\ index.php处执行。

My PHP code : 我的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 file 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 .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]

I tried to execute POST & GET of categories in Google Advanced REST Client, but I keep on getting 404. 我试图在Google Advanced REST Client中执行categories POSTGET ,但是我一直在获取404。

RESPONSE: 响应:

<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>

Any idea why am I getting this error only. 知道为什么我只会收到此错误。
Wamp is running on port 81. I can execute phpadmin perfectly well. Wamp在端口81上运行。我可以很好地执行phpadmin。 Why this is causing problem, can't figure out since days. 为什么这会引起问题,至今已无法弄清楚。

Can you please help me fix this error. 您能帮我解决此错误吗?

Any help is highly appreciative. 任何帮助都是高度赞赏的。

Thanks a lot. 非常感谢。

Looks like the issue could be a rewrite problem. 看起来该问题可能是重写问题。 I see your rewrite rule is commented out in your htaccess file. 我看到您的htaccess文件中注释了您的重写规则。 You want to route everythig to the index.php right? 您想将Everythig路由到index.php对吗?

You can probably test that this is the issue by visiting ...index.php/categories and that page should load. 您可以通过访问... index.php / categories来测试这是否是问题所在,并且该页面应该加载。

EDIT: try this htaccess file. 编辑:尝试此htaccess文件。

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

As @Steve Parish has said, it looks like you don't have your URL rewriting setup/enabled. 正如@Steve Parish所说的,您似乎没有设置/启用URL重写。 While Steve is helping you with getting your rewrite working properly, I would also recommend checking that the module required for rewrite to work is enabled. 在史蒂夫(Steve)帮助您使重写正常工作的同时,我还建议您检查是否启用了重写工作所需的模块。 Try out the answer here to check that mod_rewrite is enabled: How to check if mod_rewrite is enabled in php? 在这里尝试答案以检查是否启用了mod_rewrite: 如何检查是否在php中启用了mod_rewrite?

If it isn't, your .htaccess would essentially be ignored. 如果不是,则基本上将忽略您的.htaccess。 Best to check this before fighting htaccess syntax that should be working. 最好在应使用htaccess语法之前进行检查。 Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM