MAMP 奇怪的行为:php 从 http:// 读取外部文件非常慢,但从 https:// 读取外部文件很快

MAMP strange behaviour : php read external file from an http:// is very slow, but from https:// is quick

我有一个简单的 PHP 脚本来逐行读取远程文件,然后 JSON 对其进行解码。在生产服务器上一切正常,但在我的本地机器上(MAMP 堆栈,OSX)PHP 挂起。它非常慢,生成 JSON 文件需要 2 多分钟。我认为是 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

感谢您的宝贵时间。

我找到了原因。是路径协议。

$filename = 'http://www.yyy/response.json';

它冻结了服务器 1 到 2 分钟。 我用https协议把文件换到另一台服务器上,用了

$filename = 'https://www.yyy/response.json';

而且有效。