3

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 ?

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'
        ]);
    }

1 Answer 1

1

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');
    //....


}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you, i change my code with Amp/Http/Client and it's ok

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.