Jump to content

Curl Not Whole Content


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.