替代CURLOPT_RANGE来抓取特定部分 [英] Alternative to CURLOPT_RANGE to grab a specific section

查看:510
本文介绍了替代CURLOPT_RANGE来抓取特定部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用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

这篇关于替代CURLOPT_RANGE来抓取特定部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆