我有一個簡單的 PHP 腳本來逐行讀取遠程文件,然后對其進行 JSON 解碼。在生產服務器上一切正常,但在我的本地計算機(MAMP 堆棧、OSX)上 PHP 掛起。它非常慢,需要 2 分鐘以上才能生成 JSON 文件。我認為這是json_decode()凍結的。為什么只在 MAMP 上?我認為它陷入了while循環,因為我無法顯示$str作為所有行結果的最終變量。如果您想知道為什么我需要逐行讀取文件,那是因為在真實場景中,遠程 JSON 文件是一個 40MB 的文本文件。我唯一好的表現結果就是這樣,但是有什么好的建議嗎?有沒有配置php.ini可以幫助解決這個問題?// The path to the JSON File$fileName = 'http://www.xxxx.xxx/response-single.json'; //Open the file in "reading only" mode.$fileHandle = fopen($fileName, "r"); //If we failed to get a file handle, throw an Exception.if($fileHandle === false){ error_log("erro handle"); throw new Exception('Could not get file handle for: ' . $fileName);} //While we haven't reach the end of the file.$str = "";while(!feof($fileHandle)) { //Read the current line in. $line = fgets($fileHandle); $str .= $line;} //Finally, close the file handle.fclose($fileHandle); $json = json_decode($str, true); // decode the JSON into an associative array謝謝你的時間。
1 回答

拉丁的傳說
TA貢獻1789條經驗 獲得超8個贊
我找到了原因。它是路徑協議。
和
$filename = 'http://www.yyy/response.json';
它會使服務器凍結 1 到 2 分鐘。我將文件更改為使用https協議的另一臺服務器,并使用
$filename = 'https://www.yyy/response.json';
它有效。
- 1 回答
- 0 關注
- 103 瀏覽
添加回答
舉報
0/150
提交
取消