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

问题描述

我有一个简单的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

感谢您的时间。

解决方法

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

使用

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

它将冻结服务器1到2分钟。 我将文件更改为使用https协议的另一台服务器,并使用了

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

它有效。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...