user636044
user636044

Reputation:

How to access POST parameters in the Phalcon Micro framework

I'm trying out the Phalcon micro framework. The tutorial on this page only mentions the following way to access request data:

$app->request->getJsonRawBody();

I really just want to access standard POST parameters but since I don't see that in the tutorial, I tried passing some JSON in the request body. As I result I got a 500 error and this in my log:

PHP Fatal error:  Call to a member function getJsonRawBody() on a non-object in /Users/tom/Dropbox/Code/microphalcon/index.php on line 8
PHP Stack trace:
PHP   1. {main}() /Users/tom/Dropbox/Code/microphalcon/index.php:0
PHP   2. Phalcon\Mvc\Micro->handle() /Users/tom/Dropbox/Code/microphalcon/index.php:44
PHP   3. {closure:/Users/tom/Dropbox/Code/microphalcon/index.php:6-11}() /Users/tom/Dropbox/Code/microphalcon/index.php:44

Google has not helped.

All I want to do is access the POST parameters. How can I do that?

Upvotes: 0

Views: 11149

Answers (4)

Karl Buys
Karl Buys

Reputation: 449

I know this question is old, but was looking for the solution myself. I post it here so that it might help future users.

The original asker wanted to know how to access POST data, and the correct way to do so is to invoke getPost on the app's request object, with NO parameters.

$myPostArray = $app->request->getPost();

This will return the contents of PHP's POST array.

Upvotes: 3

noob
noob

Reputation: 78

I download PhalconRest example to create RESTful API.This is what I did to get POST data.

public function post()
{
    $request = $this->di->get('request');
    $data = $request->getJsonRawBody();
    return array($data->{'test'});
}

Test:

curl -i -X POST -d {\"test\":\"le\"} http://localhost/app/test

Here is the result:

HTTP/1.1 200 OK
Date: Sun, 14 Sep 2014 03:32:45 GMT
Server: Apache/2.2.26 (Unix) DAV/2 PHP/5.5.16 mod_ssl/2.2.26 OpenSSL/0.9.8y
X-Powered-By: PHP/5.5.16
E-Tag: 32b0f42ce7fa825050bfbc0fd3e2f7d0
Content-Length: 57
Content-Type: application/json

{"_meta":{"status":"SUCCESS","count":1},"records":["le"]}

Upvotes: 0

ferodss
ferodss

Reputation: 668

Your $app ins't an object...are you using closure correctly?

//Adds a new robot
$app->post('/api/robots', function() use ($app) {

    $robot = $app->request->getJsonRawBody();
    // ...

Pay attention on use statement!

Upvotes: 5

BlaShadow
BlaShadow

Reputation: 11693

This is a generic answer but try this.

$payload = file_get_contents('php://input');

$data = json_decode($payload);

Upvotes: 0

Related Questions