在我的 Symfony 專案中,我創建了一個控制器和一個函式來從站點檢索 APi.json 的內容。
我正在使用 HttpClient 獲取內容并將其嵌入到專案中的新檔案中。
但是,當我呼叫此函式時,寫入新檔案時出錯:
Http2StreamException> Http2StreamException> TransportException
超出正文大小限制
這個錯誤來自這段代碼:
foreach ($httpClient->stream($response) as $chunk) {
fwrite($fileHandler, $chunk->getContent());
}
我創建了一個 php.ini:
memory_limit = '4G'
upload_max_filesize = '700M'
max_input_time = 300000 post_max_size
= '700M'
原始檔案只有 242MB,內容較大,不希望放入新檔案中。
我如何繞過此例外并允許對新檔案進行 fwrite ?
提前致謝
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'
]);
}
uj5u.com熱心網友回復:
所以你面臨的限制來自Request 類的$bodySizeLimit屬性,它有一個來自 const 的默認值。
但是你可以“解鎖”它,因為回購本身中的這個例子試圖解釋
所以基本上,你可以像這樣調整你的代碼:
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');
//....
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/368138.html
