api - php slim array wrong response -
hi guys i'm trying json response server, server send wrong array response, wrong, why wring response:
{ "error": false, "status": 200, "news": { "0": { "id": 30 }, "1": { "id": 28 }, "2": { "id": 32 }, "3": { "id": 31 } } }
this php code:
<? require_once '../include/dbhandler.php'; require '.././libs/slim/slim.php'; \slim\slim::registerautoloader(); $app = new \slim\slim(); $app->get('/main', function() { $response = array(); $db = new dbhandler(); $result = $db->getmainnews(); $response["error"] = false; $response["status"] = 200; $response["news"] = array(); while($new = $result->fetch_assoc()) { $tmp = array(); $tmp["id"] = $new["id"]; array_push($response["news"], $tmp); } } ); $app->run();
you trying manually setup response when should doing using embedded response object.
see examples in slim manual:
for example:
$app->response->headers->set('content-type', 'application/json'); $return = array(); $db = new dbhandler(); $result = $db->getmainnews(); $return["news"] = array(); while($new = $result->fetch_assoc()) { $tmp = array(); $tmp["id"] = $new["id"]; array_push($return["news"], $tmp); } $app->response->body(json_encode($return));
Comments
Post a Comment