Botnary / wp-slim-framework

Slim framework as Wordpress plugin
73 stars 26 forks source link

Using post data #6

Closed fi5u closed 9 years ago

fi5u commented 9 years ago

Hi, I'm just getting set up with this plugin and I'm wondering how I pass Post data through and then use it from the slim_mapping function.

I'm querying the api with javascript, (specifically Angular):

req = {
    method: 'POST',
    url: CONSTANTS.external.server + '/slim/api/posts',
    headers: {
        'Content-Type': 'application/json'
    },
    data: {
        'abc': '123',
        'def': '456'
    }
}

$http(req).success(function(response, status) {
...

Then in the PHP file, the slim_mapping function looks like:

public function slim_mapping($slim) {
    $context = $this;

    $slim->post('/slim/api/posts', function($data)use($context) {
        $context->addPost($data);
    });
}

private function addPost($data) {
    echo json_encode($data);
}

How do I get hold of the passed data within the data object?

Botnary commented 9 years ago
req = {
    method: 'POST',
    url: CONSTANTS.external.server + '/slim/api/posts',
    headers: {
        'Content-Type': 'application/json'
    },
    data: JSON.stringify({
        'abc': '123',
        'def': '456'
    })
}

try to stringify your object with json and send it as plain text. so your request will look like this:

POST /slim/api/posts HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (iPad; CPU OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B554a Safari/9537.53
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Content-Type: application/json; charset=UTF-8
X-Requested-With: XMLHttpRequest
Content-Length: 24
Connection: keep-alive

{"abc": 123, "def": 456}
fi5u commented 9 years ago

Thanks, but it seems like when I try to pass any data, I get a 404 error with: XMLHttpRequest cannot load http://dev.site/slim/api/posts. If I don't pass the data, I don't get the error. Any ideas what might be causing this issue?

Botnary commented 9 years ago

In web master tools when you do your request and you don't get 404 what TYPE it says ? POST or GET. Maybe your XMLHttpRequest wrapper when it has an empty body is using GET ?

fi5u commented 9 years ago

It was going through as POST but I found that it didn't seem to like the header content type application/json. So I sent it as text/plain and I no longer get the error. However, I'm also still not able to process the data. Checking the Slim documentation again it seemed the code in $slim->post() was wrong. The following is what I currently have, but it just returns a 500 error.

public function slim_mapping($slim) {
    $context = $this;
    $slim->post('/slim/api/posts', function()use($context) {
        $data = $slim->request->getBody();
        $context->addPost($data);
    });
}
Botnary commented 9 years ago

and you checked the Apache errors.log and found no clues why you get that 500 response ? and what do you do with data ? do you json_decode it ?

fi5u commented 9 years ago

Nothing is logged to Apache error log when the request is made. I'm almost certain the problem lies with how I'm getting the data within the PHP code. If I replace the line $data = $slim->request->getBody(); in slim_mapping() with $data = 'abc' then abc is returned back to the JavaScript calling function with a 200 response. This says to me that the above line is producing an error. Is it correct that it is $slim->request->getBody()?

Botnary commented 9 years ago

yes you are right, to have access to the $slim object in the callback function you need to add it to the use($context, $slim) and now you have access to $slim object.

public function slim_mapping($slim) {
    $context = $this;
    $slim->post('/slim/api/posts', function()use($context, $slim) {
        $data = $slim->request->getBody();
        $context->addPost($data);
    });
}
fi5u commented 9 years ago

Yes, that works! Many thanks for working through it with me.