I am having a problem with saving a file from a response to a POST request.
I am using Google Charts API to create a chart using a POST request. I am then trying to save the result as an image.
I am following the API documentation as described here: http://code.google.com/apis/chart/docs/post_requests.html
Here is my code:
$file = fopen($url, 'r', false, $context);
$file2 = fopen("test.png", 'w');
while (!feof($file)) {
$buffer = fgets($file, 8192);
fwrite($file2, $buffer);
}
fclose($file);
It saves the image partially, up to 20k or so, leaving the bottom part of the image unsaved.
(edit) Here is the working code, using curl:
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_FILE, $file);
curl_setopt($handle,CURLOPT_POST,count($request));
curl_setopt($handle, CURLOPT_POSTFIELDS, $request_string);
$data = curl_exec($handle);
Not sure it’ll help, but what if you try using
fread, instead offgets— the first being “binary safe”, while the second stops at line ends… Considering you’re trying to read from a binary stream, and not a text-file, maybe that can change something ?If it doesn’t change a thing, another idea would be to try using
file_get_contents, to download the file as a whole, instead of having to do some kind of loop.And if it still doesn’t work, what about
curl?