替代 CURLOPT_RANGE 以获取特定部分

2022-01-24 00:00:00 range curl php

我正在尝试使用 curl 仅获取页面的一部分,这样它会下载更少的数据,从而使其更快.我一直在测试我能想到的所有可能的选择,但无济于事.我尝试过的主要方法是定义一个范围:curl_setopt($ch, CURLOPT_RANGE, "0-4096");

I'm trying to use curl to fetch only a portion of a page so it will download less data thus making it quicker. I've been testing every possible option i can think of to no avail. The main one ive tried is defining a range: curl_setopt($ch, CURLOPT_RANGE, "0-4096");

我尝试使用的服务器是 HTTP 1.1,但该设置在整个页面被拉出时无效.是否有另一种方法可以在 PHP 中的 X 字节或类似的内容之后关闭连接?

The servers im trying this on are HTTP 1.1 but the setting has no effect as the entire page is pulled. Is there an alternative way to close the connection after X bytes in PHP or something along those lines?

推荐答案

您可以使用自己的写回调 (CURLOPT_WRITEFUNCTION) 并在收到足够的数据后让它返回错误.

You can use your own write callback (CURLOPT_WRITEFUNCTION) and have that return an error once you've received enough data.

可以在此处找到使用此类写入回调的示例:http://curl.haxx.se/libcurl/php/examples/callbacks.html

An example using such a write callback can be found here: http://curl.haxx.se/libcurl/php/examples/callbacks.html

相关文章