doudanglang5826 2016-11-12 07:29
浏览 25
已采纳

设置Guzzle要检索的最大内容量

I want to download the first 16 KB of a given URL and I'm using the GuzzleHTTP library for this. Checking the Content-Length header or sending the Ranges header will not work for my purposes, since the server may not send/accept any of these headers.

Is there a way I can set the maximum amount of content that should be retrieved by Guzzle?

  • 写回答

1条回答 默认 最新

  • dongqie2010 2016-11-13 09:12
    关注

    Yes, there is a way. Use stream option to download content by chunks, not all at once.

    $response = $client->request('GET', '/stream/20', ['stream' => true]);
    // Read bytes off of the stream until the end of the stream is reached
    $body = $response->getBody();
    $buffer = '';
    while (!$body->eof() && (strlen($buffer) < 16384)) {
        $buffer .= $body->read(1024);
    }
    $body->close();
    

    With this solution you are able to control the amount of content you want to download.

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

报告相同问题?