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 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题