How to access data sent by PUT (ajax) in php?

Asked

Viewed 199 times

1

I am creating an application in REST format in which my front-end requests a PUT (By ajax) to the back-end. No request I am sending the new information for update, but I am not able to access this information in PHP, I did a research on the subject, I know that PHP has not defined the super global $_PUT as the $_POST. I came across people searching the data from php://input but when I perform the procedure parse_str(file_get_contents('php://input'), $teste); the return does not come in Array. Has anyone ever gone through this and can give me a light? Note: I am not using backend frameworks.

PHP code I have a file of routes that calls the method.

    public static function update($param){
        parse_str(file_get_contents('php://input'), $teste);
        echo "<pre>";
        var_dump($teste);
        exit;

    }

Javascript code

Method for making PUT calls

function putAjax(uri, data, callback) {
    $.ajax({
        url: uri,
        type: "PUT",
        dataType: "json",
        data: data,
        cache: false,
        contentType: false,
        processData: false
    }).done(function (data) {
        if (callback) {
            for (var i = 0; i < callback.length; i++) {
                callback[i](data);
            }
        }
    }).fail(function () {
        fechaLoading();
        return false;
    });
}

Method that is calling the putAjax

function updateServidores(form) {

    var verificaRetorno = function (data) {
        if (data.status) {
            if (data.status === true) {
                servidor.splice(indiceUpdateServidores, 1);
                servidor.push(data.data[0]);
                montaGrid(".tableServidores");
                abreTela("#viewServidores");

                notify("success", "Sucesso!", "Item alterado com sucesso!");

            }else {
                notify("error", "Erro!", data.erro.message);
            }

            indiceUpdateServidores = null;
        }
    };

    after = [];
    after.push(verificaRetorno);

    var formData = new FormData(form);
    putAjax(url + "servidor/" + servidor[indiceUpdateServidores].ID_SERVIDORES, formData, after);
}

Exit from file_get_contents('php://input'):

------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="nome"

deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="servidor"

deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="porta"

deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="email"

deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="senha"

deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="email_envio"

deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="reply"

deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr
Content-Disposition: form-data; name="alias"

deletaServidor(this);
------WebKitFormBoundarynQKeRELA6A6uJMkr--
  • 3

    put the code there to know what you’ve done

  • @Weessmith I added the code to the text.

  • @Lucasjunior take a look at the link that Luizfelipe spoke there

  • @Luizfelipe and @Weessmith I tested the described solution unsuccessfully. I am receiving an array with a single string with all data from php://input

  • The question talks about getting data from PUT, but in fact the question is about how to parse the form (and in the comments of the answer that speaks in JSON it was commented that the solution was to change the format of the data, no longer reflecting the problem exposed). In this way, I do not see how the question and answer can serve other visitors, so it was closed.

1 answer

2

If you are receiving the data in the backend in JSON format, you should use the function json_decode and not parse_str:

public static function update($param){
    $teste = json_decode(file_get_contents('php://input'));
    echo "<pre>";
    var_dump($teste);
    exit;
}

Thus the variable $teste will contain an object or array depending on what was sent

You can use $teste = json_decode(file_get_contents('php://input'), true); so that objects are transformed into associative arrays

  • when doing this I get in return NULL.

  • The var_dump showcase null? So what’s the exit from var_dump(file_get_contents('php://input'));? Add to question

  • I added var_dump output as requested..

  • 1

    In the ajax call add the configuration contentType: 'application/json'. Must solve

  • With your comment I anticipated the other thing. Mine data was being generated by a FormData performed data conversion to json format and added contentType: 'application/json'. Problem solved.

  • 1

    Exactly, if a format is not reported, it is sent that way. You can use other formats and decode the input appropriately

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.