Home > Enterprise >  Symfony HttpClient Stream and chunk->getContent() Body size limit exceeded
Symfony HttpClient Stream and chunk->getContent() Body size limit exceeded

Time:11-28


In my Symfony project, I created a controller and a function to retrieve the content of an APi.json from a site.

I am using HttpClient to grab the content and embed it into a new file in the project.

However when I call this function, I have an error writing the new file:

Http2StreamException> Http2StreamException> TransportException
Body size limit exceeded

this error comes from this piece of code :

foreach ($httpClient->stream($response) as $chunk) {
            fwrite($fileHandler, $chunk->getContent());
        }

I created a php.ini with:
memory_limit = '4G'
upload_max_filesize = '700M'
max_input_time = 300000
post_max_size = '700M'

The original file weighs only 242MB and the content does not want to fit into the new file because of its fairly large content.
How I can bypass this Exception and allow fwrite on the new file ?

thanks in advance

public function infoBDD(): Response 
{
        //Update le fichier sur le site
        $httpClient = HttpClient::create();
        $response = $httpClient->request('GET', 'https://mtgjson.com/api/v5/AllPrintings.json');

        // Création du fichier
        $fileHandler = fopen('../public/BDD/Api.json', 'w');

        // Incorporation dans le fichier créé le contenu du fichier uploadé
        foreach ($httpClient->stream($response) as $chunk) {
            fwrite($fileHandler, $chunk->getContent());
        }

        //fermeture du fichier créé
        fclose($fileHandler);

        var_dump('ouverture nouveau fichier');
        //Ouverture du fichier voulu
        $content = file_get_contents('../public/BDD/Api.json');
        $data = json_decode($content, true);

        //Vérification si la clé 'data' n'existe pas
        if(!array_key_exists('data', $data)) {
            throw new ServiceUnavailableHttpException("La clé 'data' n'existe pas dans le tableau de données récupéré,
            la réponse type fournie par l'API a peut-être été modifiée");
        }

        //Vérification si la clé 'data' existe
        if(array_key_exists('data', $data)) {
            $api = $data['data'];
            $this->getTableauData($api);
        }

        unlink('../public/BDD/Api.json');

        return $this->render('users/index.html.twig', [
            'controller_name' => 'UsersController',
            'page' => 'Profile'
        ]);
    }

CodePudding user response:

So the limit you are facing comes from the $bodySizeLimit property of the Request class, which has a default value from a const there.

But you can "unlock" it, as this example in the repo itself tries to explain

so basically, you might adjust your code like this:

        public function infoBDD(): Response 
        {
            // Instantiate the HTTP client
            $httpClient = HttpClientBuilder::buildDefault();
    
            $request = new Request('https://mtgjson.com/api/v5/AllPrintings.json');
            $request->setBodySizeLimit(242 * 1024 * 1024); // 128 MB
            $request->setTransferTimeout(120 * 1000); // 120 seconds
            $response = $httpClient->request('GET', 'https://mtgjson.com/api/v5/AllPrintings.json');
    //....


}
  • Related