RESTful:通过PUT和DELETE发送数据的方式


RESTful: how to send data through PUT and DELETE

我决定以良好实践的名义开始使用RESTful url。然而,像往常一样,斗争没过多久就开始了。

所以据我所知,我应该使用PUT来更新,但是在PHP中没有$_PUT超全局。因此,这导致了一个高度科学的实验,包括用PUT方法创建一个表单,目的是找出PUT数据是如何发送的。我很惊讶在URL中发现了GET参数。

必须有一些东西,我错过了,因为有只是没有办法我可以使用数据从URL更新数据库或其他东西。如果我要求用户输入他们的密码来更新他们的设置呢?这是无法形容的。谁能给我点化一下吗?

正如我所想的,我能够从HTML表单发送PUT和DELETE请求,但它不会导致PUT,而是导致GET请求。

为了使用HTML形式的rest式url,我会使用POST请求和像这样的东西

// JavaScript
window.onload = function(){
    var f = document.getElementById('form');
    f.button.onclick = function(){
        var username = f.username.value;
        var password = f.password.value;
        // in a final application I would use a form serializer
        xhr = new XMLHttpRequest();
        var url = "http://www.domain.com/restful/";
        xhr.open("POST", url, true);
        xhr.setRequestHeader("Content-type", "application/json");
        xhr.onreadystatechange = function () { 
            if (xhr.readyState == 4 && xhr.status == 200) {
                console.log(xhr.responseText);
                // you may return a JSON as well
                // var json = JSON.parse(xhr.responseText);
                // console.log(json.username);
            }
        }
        var data = JSON.stringify({"username":"" + username + "","password":"" + password + ""});
        xhr.send(data);
    }
}
<!-- HTML form -->
<form id="form" method="POST" action="">
    <input type="text" name="username" value="" />
    <input type="password" name="password" value="" />
    <input type="button" name="button" value="Submit" />
</form>
// PHP
header("Content-Type: application/json");
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    $input = json_decode(stripslashes(file_get_contents("php://input")), true);
    echo $input['username'];
    // in case you return a JSON, encode data first
    // echo json_encode($input);
}

.htaccess保持不变


(最初的回答)

这样你就可以构建PUT请求

$url = 'http://www.domain.com/restful/';
$data = array('username'=>'hey@mail.com','password'=>'101010');
$data_json = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
curl_close($ch);
echo $response;

或者DELETE请求,像这样

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response  = curl_exec($ch);
curl_close($ch);

这是restful目录下的index.php文件

header("Content-Type: application/json");
if($_SERVER['REQUEST_METHOD'] == 'PUT') {
    $input = json_decode(file_get_contents("php://input")); 
    echo $input->username;
}elseif($_SERVER['REQUEST_METHOD'] == 'DELETE'){
    // ...
}

您可以使用resful目录下的.htaccess将所有内容重定向到index.php

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L]