Search code examples
phpiis-7http-verbs

PHP IIS7 VERB PUT get data


I have another question similar to a question I asked here: JS Ajax calling PHP and getting ajax call data

However this time I'm dealing with the PUT verb. I was reading to get put data in php should use the following:

file_get_contents("php://input")

Resource: http://www.lornajane.net/posts/2008/accessing-incoming-put-data-from-php

However for put this dosen't seem to be working. I feel like it might have something to do with IIS 7 possibly removing the data?.. I had webDav installed and had to remove that to get the put verb to resolve was wondering is there something else in IIS that could be preventing the data from being parsed at the server level?

My ajax request looks like the following

                var data = '{"storyId":"2","storyName":"a Changed Story.","authorId":"5", "published":"1"}'; 

                $.ajax({
                    type: "PUT",
                    url: BaseUrl + "Story/2",   
                    data: data,
                    success: function(data){
                        console.log(data);
                    },
                    error: function(request){
                        console.log(request);
                    },
                });

Solution

  • WooHoo I figured it out thanks to this question.

    PHP get PUT request body

    For some reason if you request data for "PUT" like this

    file_get_contents("php://input")
    

    On the first read it will get all the data. However the second attempt to get data using it like this will return null. This is not the case with POST. I simply had to make it so that we only request the data like the above once.

    MY Theory (could be BS) ; ) I think the way it reads PUT request is like reading a file or a stream if there is a way to set the reader to the start again probably could get that data. This is just my theory on how this works I don't really have any resources backing that this works like that.