0

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;
Andre Garcia
  • 894
  • 11
  • 30

2 Answers2

0
return $response->write('{"json":"message"}')
          ->withHeader('Content-Type', 'application/json');

Just write it directly to the stream, and don't forget to set the proper content-type :)

geggleto
  • 2,605
  • 1
  • 15
  • 18
  • Ahh i forgot to mention the json it's multi-dimensional. It works if i put return $response->write($result[0]) ->withHeader('Content-Type', 'application/json'); It return the already formatted json but not the entire set. :/ – Andre Garcia Jul 22 '16 at 15:47
  • Possibly i need to loop the json to send as a param to the write method.. for that i would need to decode and then encode again...:/ Am i right? – Andre Garcia Jul 22 '16 at 16:04
  • Yes. you would need to do that ;( – geggleto Jul 22 '16 at 16:51
  • $lenght = count($result); for ($i=0; $i<$lenght; $i++){ $response->write($result[$i])->withHeader('Content-Type', 'application/json'); } return $response->getBody(); – Andre Garcia Jul 22 '16 at 17:05
  • i tried that method... and it returns the data but he returns some how a malformed json... god dammit! :) It's a huge dataset to decode and encode again...:/ – Andre Garcia Jul 22 '16 at 17:07
  • is it possible to not decode it the first time? or are you accessing an API that spits out json ? ... I want to say there should be a way to walk a large json structure... but i dont know of one yet. – geggleto Jul 22 '16 at 17:20
  • i work with gearman workers and i must return a json for each job they do. Decoding it into a variable and return the result encoded is giving me performance issues along with memory exaustion.. – Andre Garcia Jul 22 '16 at 17:30
  • can you pop into Slim IRC or our Slack and maybe I can help out some more – geggleto Jul 22 '16 at 17:40
0

I figure how i could return a already formatted JSON. I needed to append "," and "[]" to the response otherwise the data wouldn't be in a correct JSON syntax.

This is what i came with:

$result = $forecast->runForecast(true);        
$lenght = count($result);              

    for ($i=0; $i<$lenght; $i++){         
        if ($i == 0){
            $response->write('['.$result[$i].',');                     
        }
        elseif($i == $lenght - 1)
        {
            $response->write($result[$i].']');
        }
        else
        {
            $response->write($result[$i].',');                        
        }
    }          

$newResponse = $response->withHeader('Content-type', 'application/json; charset=utf-8');
return $newResponse;
Andre Garcia
  • 894
  • 11
  • 30