我正在 PHP 中處理一個大型 JSON 數據文件 (25 mb)。
現在,我可以獲取該文件并檢查它的字符串長度,盡管我無法 echo
輸出該字符串,但我得到了 24479798。
在 echo strlen()
之后,腳本的其余部分崩潰了,我沒有得到進一步的輸出,包括 echo "Made it to the Bottom";
為什么我無法讓 json_decode
工作?這是腳本內存問題,還是JSON數據中的字符編碼問題?我被困住了。
<?php error_reporting(E_ALL); ini_set('error_reporting', E_ALL); if($result = file_get_contents("test.json")){ echo "GOT IT"; }else{ echo "NO"; } // This works echo "Got to here"; // This works echo strlen($result); // I get "24479798", which is the file string length var_dump($diffbotResult); // Outputs all the JSON data, so I know I have it and it's proper JSON data $result = json_decode($result, true); echo json_last_error_msg(); // No output print_r($result); // No output echo "Made it to the bottom"; // Does not echo out anything ?>
考慮到您嘗試導入大文件,腳本的運行時間比瀏覽器/服務器所需的時間長,并且會在完成之前超時。像這樣的腳本需要在命令行或通過 cron 運行來消除超時。您還可以通過在腳本開頭添加 ini_set('memory_limit', '512M');
(或根據需要更高的值)來增加內存,以確保它有足夠的內存來加載和處理json 文件。