Im using Slim Framework to return a result already in a JSON format.
$app->get('/forecast_range/{latitude}/{longitude}/{timeStart}/{timeEnd}', function (Request $request, Response $response) {
$latitude = $request->getAttribute('latitude');
$longitude = $request->getAttribute('longitude');
$timeStart = $request->getAttribute('timeStart');
$timeEnd = $request->getAttribute('timeEnd');
$timeStart = new DateTime($timeStart);
$timeEnd = new DateTime($timeEnd);
$coordinates[] = array('latitude' => $latitude, 'longitude' => $longitude);
$forecast = new forecast_range_url($coordinates, 1, $timeStart, $timeEnd);
$result = $forecast->runForecast(true);
return $response->withJson($result);
});
The $result
variable is already a JSON, a multidimensional one. How i can i return to the client the $result variable without need to encode it again?
Im trying to use this code to append the $result
keysJSON to the response. I feel im close but not yet. I'm getting a syntax error.
$lenght = count($result);
for ($i=0; $i<$lenght; $i++){
$response->write($result[$i]);
}
$newResponse = $response->withHeader(
'Content-type',
'application/json; charset=utf-8'
);
return $newResponse;