方法不允许PUT和DELETE

时间:2017-11-19 11:33:36

标签: php android frameworks slim

我正在使用SLIM Framework开发我在Android应用程序中使用的Web服务,但我正在使用Postman测试它们。

我的GET和POST方法工作正常,但是当我尝试调用PUT或DELETE时,它在我的浏览器中显示一条消息告诉我该方法不允许应该是PUT之一或者如果是删除请求,请告诉我应该删除。

我无法找到纠正此问题的方法。

1 个答案:

答案 0 :(得分:0)

我班上的要求:

String url = "http://paulomaranhao.000webhostapp.com/myslim2/temperaturas/" + String.valueOf(item);

                Map<String, String> jsonParams = new HashMap<>();
                jsonParams.put("local", localName.getText().toString());
                jsonParams.put("continente_id", String.valueOf(continenteID));
                jsonParams.put("temp1", temp1.getText().toString());
                jsonParams.put("temp2", temp2.getText().toString());
                jsonParams.put("temp3", temp3.getText().toString());
                jsonParams.put("temp4", temp4.getText().toString());
                jsonParams.put("temp5", temp5.getText().toString());
                jsonParams.put("temp6", temp6.getText().toString());
                jsonParams.put("temp7", temp7.getText().toString());
                jsonParams.put("temp8", temp8.getText().toString());
                jsonParams.put("temp9", temp9.getText().toString());
                jsonParams.put("temp10", temp10.getText().toString());
                jsonParams.put("temp11", temp11.getText().toString());
                jsonParams.put("temp12", temp12.getText().toString());

                JsonObjectRequest putRequest = new JsonObjectRequest
                        (Request.Method.PUT, url,
                                new JSONObject(jsonParams),
                                new Response.Listener<JSONObject>() {

                                    @Override
                                    public void onResponse(JSONObject response) {
                                        try{
                                            if(response.getBoolean("status")){
                                                Toast.makeText(NewLocal.this, response.getString("MSG"), Toast.LENGTH_SHORT).show();
                                            }else{
                                                Toast.makeText(NewLocal.this, response.getString("MSG"), Toast.LENGTH_SHORT).show();
                                            }
                                        }catch(JSONException e){}
                                    }
                                }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(NewLocal.this, error.getMessage(), Toast.LENGTH_SHORT). show();
                                return;
                            }
                        }){

                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                        HashMap<String, String> headers = new HashMap<>();
                        headers.put("Content-type", "application/json; charset=utf-8");
                        headers.put("User-agent", System.getProperty("http.agent"));
                        return headers;
                    }
                };

                // Access the RequestQueue through your singleton class.
                MySingleton.getInstance(this).addToRequestQueue(putRequest);

我的网络服务:

$app->put('/api/temperaturas/local_id/{local_id}', function($request){
require_once('db/dbconnect.php');
$id = $request->getAttribute('local_id'); 
$local = $request->getParsedBody()['local'];
$continente_id = $request->getParsedBody()['continente_id'];
$temp1 = $request->getParsedBody()['temp1'];
$temp2 = $request->getParsedBody()['temp2'];
$temp3 = $request->getParsedBody()['temp3'];
$temp4 = $request->getParsedBody()['temp4'];
$temp5 = $request->getParsedBody()['temp5'];
$temp6 = $request->getParsedBody()['temp6'];
$temp7 = $request->getParsedBody()['temp7'];
$temp8 = $request->getParsedBody()['temp8'];
$temp9 = $request->getParsedBody()['temp9'];
$temp10 = $request->getParsedBody()['temp10'];
$temp11 = $request->getParsedBody()['temp11'];
$temp12 = $request->getParsedBody()['temp12'];
$temperaturas = array($temp1, $temp2, $temp3, $temp4, $temp5, $temp6, $temp7, $temp8, $temp9, $temp10, $temp11, $temp12);
try{
    $tabela_temperaturas = $db->local_mes();
    $tabela_local = $db->local();
    $local_data = array("nome" => $local, "continente_id" => $continente_id);
    $firstResult = $tabela_local[$id]->update($local_data);
    for($i = 0; $i < 12; $i++){
        $data = array("local_id" => $firstResult["id"], 
                        "mes_id" => $i+1, 
                            "temperatura" => $temperaturas[$i]);
    }
    $result = $tabela_temperaturas[$id]->update($data); 
}catch(Exception $e){
    $result = ['status' => false, 'MSG' => $e->getMessage()];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
}
if($result){
    $result = ['status' => true, 'MSG' => "Atualizado com sucesso!"];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
}else{
    $result = ['status' => false, 'MSG' => "Erro na atualização!"];
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
}

});

我的index.php

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;   // objetos do tipo pedido que usam o psr por causa do http
 use \Psr\Http\Message\ResponseInterface as Response;       // objetos do tipo resposta que usam o psr por causa do http

 require 'vendor/autoload.php';  // ficheiro criado pelo composer e é necessário por causa das dependencias do slim

 $settings =  [
  'settings' => [
         'displayErrorDetails' => true,
    ],
 ];

 $app = new \Slim\App($settings);  // o objeto principal slim para podermos 
 usar os nossos endpoints

 require_once('api/localmes.php');
 require_once('api/local.php');
 require_once('api/continente.php');

$app->run();
?>