在PHP中读取远程页面的一部分

在PHP中读取远程页面的一部分,php,curl,file-get-contents,Php,Curl,File Get Contents,我在URLhttp://site.com/params.我只想从这个远程页面读取前n个字符。像readfile()、file\u get\u contents()和curl这样的函数似乎可以下载整个页面。无法理解如何在PHP中实现这一点 请帮忙 您可以尝试通过套接字执行此操作 如果使用了maxlen参数,则可能就是您想要的。默认情况下,此功能: //Reads entire file into a string $wholePage= file_get_contents('http://www.

我在URLhttp://site.com/params.我只想从这个远程页面读取前n个字符。像
readfile()
file\u get\u contents()
curl
这样的函数似乎可以下载整个页面。无法理解如何在PHP中实现这一点


请帮忙

您可以尝试通过套接字执行此操作

如果使用了
maxlen
参数,则可能就是您想要的。默认情况下,此功能:

//Reads entire file into a string
$wholePage= file_get_contents('http://www.example.com/');
但是,maxlen参数是数据读取的最大长度


这意味着不读取整个文件,如果定义了
maxlen
,则只读取
maxlen
字符。

阅读@Josh的
偏移量和
maxlen
参数,但这不就是下载整个页面,然后给我前n个字符吗?如果我错了,请纠正我……我无法想象会是这样。默认情况下,它最多只能读取
maxlen
个字符或全部字符。“我会让其他人证明这一点的。”乔希,请你解释一下,并把它作为一个答案贴出来。感谢使用带有url的fopen假定php.ini中的
allow\u url\u fopen
已在@yAnTar启用,对不起,您提到的代码将下载整个页面,这是我不想做的。你能解释一下这里如何使用插座吗?@ahmedtabrez没问题。记住,这就是我对它的理解。
//Reads entire file into a string
$wholePage= file_get_contents('http://www.example.com/');
// Read 14 characters starting from the 1st character
$section = file_get_contents('http://www.example.com/', NULL, NULL, 0, 14);