亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

MAMP 奇怪的行為:php 從 http:// 讀取外部文件非常慢

MAMP 奇怪的行為:php 從 http:// 讀取外部文件非常慢

PHP
至尊寶的傳說 2023-10-15 15:34:39
我有一個簡單的 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';

它有效。


查看完整回答
反對 回復 2023-10-15
  • 1 回答
  • 0 關注
  • 103 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號