mr.incredible
mr.incredible

Reputation: 4185

PHP: file_get_contents from URL truncated

I want to get XML file (about 13MB) from URL but file_get_contents truncates the response for some reason.

I've already set php.ini for large files:

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

This code sample outputs broken piece of data:

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

enter image description here

Response size is truncated (1.3M instead of 13M):

enter image description here


If I download this XML file (13M) via browser and then parse it with PHP from local directory it works well:

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

But if I try to get the same file from URL it will not work:

$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'];

I found out that PHP request (either file_get_contents or CURL) cuts response up to 1.3M despite the fact that I've already set max upload size to 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.';

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


How to get the entire XML file from URL with PHP? I've already set php.ini. What config I missed? I spent at least 10 hours to find any reliable example but with no success. I can't believe I literally screwed up now with such simple task.

Upvotes: 0

Views: 1096

Answers (1)

user13316830
user13316830

Reputation:

The downloaded file is correct and PHP is working correctly. Your file is gz-compressed. You need to unpack it:

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

Upvotes: 3

Related Questions