We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Tutorial 7: Creating a Simple REST API problem

hello everyone!

i've copy-paste the code of the tutorial 7, and when i test the insertion of a new robot with the curl in the example i receive these messages:

HTTP/1.1 409 Conflict
Server: Apache/2.4.17 (Win32) Open SSL/1.0.2d PHP/5.6.19
X-Powered-By: PHP/5.6.19
Status: 409 Conflict
Content-Length: 510
Content-Type application/json; charset=UTF-8

Notice: Trying to get property of non object in C:\...\index.php on line 134
Notice: Trying to get property of non object in C:\...\index.php on line 135
Notice: Trying to get property of non object in C:\...\index.php on line 136
{"status":"ERROR",messages":["id is required","name is required","type is required","year is required"]}

and here the lines:

<?php

$robot = $app->request->getJsonRawBody();
$phql = "INSERT INTO Robots (name, type, year) VALUES (:name:, :type:, :year:)";

/*133*/ $status = $app->modelsManager->executeQuery($phql, array(
/* 134 */        'name' => $robot->name,
/* 135 */        'type' => $robot->type,
/* 136 */        'year' => $robot->year
    ));

someone knows what's is wrong?

Any code maybe ?

You're right! i've forgot part of the comment. i fixed.

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

It returns array, not object.

edited Mar '16

Here could be the problem!

i have tried in these ways

// this is the input command: 
curl -i -X POST -d '{"name":"C-3PO","type":"droid","year":1977}' https://localhost/restapiserver/api/robots

// first way:
$robot = $app->request->getJsonRawBody();
echo array_values($robot);

//second way:
$temp = file_get_contents('php://input');
$robot = json_decode($temp);
echo array_values($robot);

and the results are the same wrote in the first post, with this new warning before the Notice

warning: array_values() expects parameter 1 to be array, null given.

supposing a connection problem, i made a control with

//input
curl -i -X POST -d '{"name":"C-3PO","type":"droid","year":1977}' https://localhost/restapiserver/api/contact

//code
$app->post('/api/contact', function () use ($app) {
    echo "Success!";
});

returning Success!

I have no idea where is the problem and i've checked the code thinking a missing line during copy-paste, but didn't find anything wrong.

In case of need, here my files: index and robots

edited Mar '16

Oh sory getJsonRawBody can return object if no parameters passed, if you pass true then it returns array. But still it shouldn't be null.

What second way returns ?

getJsonRawBody is calling getRawBody(), in total - it's doing the same which your code does:

https://github.com/phalcon/cphalcon/blob/master/phalcon/http/request.zep#L325 https://github.com/phalcon/cphalcon/blob/master/phalcon/http/request.zep#L346

What php version you are using ? And what is your OS ?

Oh i see it's 5.6.19. Your code should work without any problem. Don't know where it's a problem, maybe it's curl problem ? The second way works ?

If you think it's phalcon problem you can clone proper repo https://github.com/phalcon/cphalcon and change code in zep to debug it with some var_dump/print_r/array_values and check where is the problem

edited Mar '16

The second way return the same result as te first (i've writte singular instead plurar, now fixed). The OS is Windows 10

and a little doubt about curl: i usede italian apostrophe ( ' ) writing the curl could be that i need to use american apostrophe ( ` )?

i'll go to try, and if doesn't work i'll try to clone proper repo

I thank you for the help :)

edited Mar '16

Then if second way return as the first then there is some problem with curl/php whatever. Maybe try to create some js formdata and send it with xhr ? Or some builtin ide REST testing(but i guess it's almost same as curl).

Try to use curl WITH:

-H "Content-Type: application/json"

Also check it:

https://stackoverflow.com/questions/813487/how-to-post-json-to-php-with-curl#comment13609539_1284087

I'm having the same problem (error) "Trying to get property of non object in" , by following the tutorial. Is thre any answer ?

i haven't found where the problem was. but i managed a way to read the json

// initially i watched which type of data i received using gettype()

// at first, with getJsonRawBody()
  $echo gettype($app->request->getJsonRawBody());
  // as result, i received a null type

//second, with 'php://input'
  $echo gettype(file_get_contents('php://input'));
  // as result, i received a string type; so, i watched the structure of the string

  $echo file_get_contents('php://input');
  // the result was structured in this way:
  // [{"id":"1","name":"Robotina"},{"id":"2","name":"Astro Boy"},{"id":"3","name":"Terminator"}]

after that, i played dirty, because the sender of the JSon is a personal application ( so i know how is structured the JSon), and i created a function to convert the string into an object.

hope it can help, Paolo

Thanks)

i haven't found where the problem was. but i managed a way to read the json

// initially i watched which type of data i received using gettype()

// at first, with getJsonRawBody()
 $echo gettype($app->request->getJsonRawBody());
 // as result, i received a null type

//second, with 'php://input'
 $echo gettype(file_get_contents('php://input'));
 // as result, i received a string type; so, i watched the structure of the string

 $echo file_get_contents('php://input');
 // the result was structured in this way:
 // [{"id":"1","name":"Robotina"},{"id":"2","name":"Astro Boy"},{"id":"3","name":"Terminator"}]

after that, i played dirty, because the sender of the JSon is a personal application ( so i know how is structured the JSon), and i created a function to convert the string into an object.

hope it can help, Paolo

edited Jun '16

To use getJsonRawBody you need to correctly send request as json https://stackoverflow.com/questions/6587221/send-json-data-with-jquery