Jump to content

Curl Not Whole Content


Guest

Recommended Posts

Can I use curl to select only a certian amount of a page instead of the whole thing. I just would like it to get the first couple of lines like 25 can i do this.

 

I use this to get the whole page

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"$url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$html=curl_exec ($ch);
curl_close ($ch);

Link to comment
https://forums.phpfreaks.com/topic/40004-curl-not-whole-content/
Share on other sites

You could use the stream/file functions to read from it as a socket, and stop at 25 lines.

So, something like:

$f = fopen('http://site.com/page.php','r');

for( $i=0;$i<25,!feof($f);$i++ )
{
    //Do something with line
    $line = fgets($f);
}

 

Probably not working code, I don't know the f* functions well for remote pages.

Yeah ShogunWarrior is right, you could use the file functions if you don't need to send post data and/or cookies (or any other stuff curl can do but the file functions can't).

 

<?php
$start_line = 10; //Set here the line to start from (first line is 0)
$amount = 11; // The amount of lines you want to read
$url = "http://www.TheDataYouNeed.com/file.txt"; //The file
//This example fetches lines 10 to 20

$end_line = $start_line + $amount;
$lines = file($url);
$count = count($lines);

$limit = ($count > $end_line) ? $end_line : $count;
$result = "";
for($i = $start_line; $i <= $limit; $i++)
$result .= $lines[$i];

echo $result;

?>

 

Orio.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.