PHP:file_get_contents 来自 URL 被截断

PHP: file_get_contents from URL truncated

我想从 URL 获取 XML 文件(大约 13MB),但是 file_get_contents 由于某种原因截断了响应。

我已经为大文件设置了 php.ini:

upload_max_filesize = 40M
post_max_size = 41M
memory_limit = 128M

此代码示例输出了不完整的数据:

$response = file_get_contents('http://example.com/file.xml');
echo $response;

响应大小被截断(1.3M 而不是 13M):


如果我通过浏览器下载这个 XML 文件 (13M),然后从本地目录用 PHP 解析它,它运行良好:

$xml = simplexml_load_file('my_file.xml');
echo $xml->Sport[0]->Country[0]->Tournament[0]->Match[0]['Team1'];

但是如果我尝试从 URL 获取相同的文件,它将不起作用:

$data = file_get_contents('http://example.com/file.xml');
$xml = simplexml_load_file($data); // <- broken truncated data
echo $xml->Sport[0]->Country[0]->Tournament[0]->Match[0]['Team1'];

我发现 PHP 请求(file_get_contents 或 CURL)将响应减少到 1.3M,尽管我已经将最大上传大小设置为 40M .

$remoteFile = 'http://example.com/file.xml';
$curl = curl_init($remoteFile);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_exec($curl);

$fileSize = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
var_dump($fileSize);

$fileSizeKB = round($fileSize / 1024);
echo 'File is ' . $fileSizeKB . ' KB in size.';

输出:float(1377225) File is 1345 KB in size.


如何使用 PHP 从 URL 获取整个 XML 文件?我已经设置 php.ini。我错过了什么配置?我花了至少 10 个小时来找到任何可靠的例子,但没有成功。我简直不敢相信我现在真的搞砸了这么简单的任务。

下载的文件正确,PHP工作正常。您的文件是 gz 压缩的。您需要解压它:

$response = file_get_contents('https://bn.wlbann.com:1034/?live=4');
$response = gzdecode($response);