dongtaogou6226 2010-02-22 04:05
浏览 63
已采纳

PHP:保存对文件的POST响应

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);
  • 写回答

1条回答 默认 最新

  • douxie7339 2010-02-22 05:30
    关注

    Not sure it'll help, but what if you try using fread, instead of fgets -- 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 ?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100