Tabrez Ahmed
Tabrez Ahmed

Reputation: 2950

Reading part of a remote page in PHP

I have a page at URL http://site.com/params. I want to read only the first n characters from this remote page. Functions like readfile(), file_get_contents() and curl seem to download whole of the pages. Can't figure out how to do this in PHP.

Please help...!

Upvotes: 2

Views: 903

Answers (2)

Josh
Josh

Reputation: 8191

file_get_contents() may be what you're looking for if the maxlen parameter is utilized. By default, this function:

//Reads entire file into a string
$wholePage= file_get_contents('http://www.example.com/');

However, the maxlen parameter is the maximum length of data read.

// Read 14 characters starting from the 1st character
$section = file_get_contents('http://www.example.com/', NULL, NULL, 0, 14);

This implies that the entire file is not read, and only maxlen characters are read, if maxlen is defined.

Upvotes: 3

yAnTar
yAnTar

Reputation: 4610

You can try to do through sockets link

$handle = fopen("http://domain.com/params", "r");
$buffer = fgets($handle, 100);
echo $buffer;
fclose($handle);

Upvotes: 2

Related Questions