我有一個(gè)簡(jiǎn)單的 PHP 腳本來(lái)逐行讀取遠(yuǎn)程文件,然后對(duì)其進(jìn)行 JSON 解碼。在生產(chǎn)服務(wù)器上一切正常,但在我的本地計(jì)算機(jī)(MAMP 堆棧、OSX)上 PHP 掛起。它非常慢,需要 2 分鐘以上才能生成 JSON 文件。我認(rèn)為這是json_decode()凍結(jié)的。為什么只在 MAMP 上?我認(rèn)為它陷入了while循環(huán),因?yàn)槲覠o(wú)法顯示$str作為所有行結(jié)果的最終變量。如果您想知道為什么我需要逐行讀取文件,那是因?yàn)樵谡鎸?shí)場(chǎng)景中,遠(yuǎn)程 JSON 文件是一個(gè) 40MB 的文本文件。我唯一好的表現(xiàn)結(jié)果就是這樣,但是有什么好的建議嗎?有沒(méi)有配置php.ini可以幫助解決這個(gè)問(wèn)題?// 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謝謝你的時(shí)間。
MAMP 奇怪的行為:php 從 http:// 讀取外部文件非常慢
至尊寶的傳說(shuō)
2023-10-15 15:34:39