php - 405 Metode ikke tilladt IIS 10 Windows Server 2016

Indlæg af Hanne Mølgaard Plasc

Problem



Jeg forsøger at lave en API til min hjemmeside og hidtil for GET og POST-metoden, det fungerede, men DELETE-metoden, jeg kan bare ikke få det til at fungere. Jeg ved, at mange mennesker har dette problem, men jeg kunne ingen steder finde løsningen. Hvis nogen kan hjælpe mig, tak!


PS: Bare for at være klar, har jeg ikke WebDav-modulet installeret på Windows Server.


Dette er mit index.php:


<?php
require\_once("DB.php");

$db = new DB("127.0.0.1", "SocialNetwork", "username", "pass");

if ($\_SERVER['REQUEST\_METHOD'] == "GET") {

    if ($\_GET['url'] == "auth") {

    } else if ($\_GET['url'] == "users") {

    }

} else if ($\_SERVER['REQUEST\_METHOD'] == "POST") {

    if ($\_GET['url'] == "auth") {
            $postBody = file\_get\_contents("php://input");
            $postBody = json\_decode($postBody);

            $username = $postBody->username;
            $password = $postBody->password;

            if ($db->query('SELECT username FROM users WHERE username=:username', array(':username'=>$username))) {
                    if (password\_verify($password, $db->query('SELECT password FROM users WHERE username=:username', array(':username'=>$username))[0]['password'])) {
                            $cstrong = True;
                            $token = bin2hex(openssl\_random\_pseudo\_bytes(64, $cstrong));
                            $user\_id = $db->query('SELECT id FROM users WHERE username=:username', array(':username'=>$username))[0]['id'];
                            $db->query('INSERT INTO login\_tokens VALUES (0, :token, :user\_id)', array(':token'=>sha1($token), ':user\_id'=>$user\_id));
                            echo '{ "Token": "'.$token.'" }';
                    } else {
                            http\_response\_code(401);
                    }
            } else {
                    http\_response\_code(401);
            }

    }

} else if ($\_SERVER['REQUEST\_METHOD'] == "DELETE") {
if ($\_GET['url'] == "auth") {
  if (isset($\_GET['token'])) {
    $db->query('DELETE FROM login\_tokens WHERE token=:token', array(':token'=>$\_GET['token']));
    echo '{ "Status": "Success" }';
    http\_response\_code(200);
  } else {
    http\_response\_code(400);
    echo '{ "Error": "Mal-formed request" }';
  }
  }

} else {
    http\_response\_code(405);
}
?>


Og det her er min web.config:


<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
    <rewrite>
        <rules>
            <rule name="API">
                <match url=".*" />
                <action type="Rewrite" url="index.php?url={R:0}" appendQueryString="false" />
            </rule>
        </rules>
    </rewrite>
    <modules>
        <remove name="WebDAVModule" />
    </modules>
    <handlers>
        <remove name="WebDAV" />
    </handlers>
    <security>
    <requestFiltering>
        <verbs allowUnlisted="false">
            <add verb="GET" allowed="true" />
            <add verb="POST" allowed="true" />
            <add verb="DELETE" allowed="true" />
            <add verb="PUT" allowed="true" />
        </verbs>
    </requestFiltering>
</security>
</system.webServer>




Bedste reference