Skip to content Skip to sidebar Skip to footer

How To Get Some Part Of A Html Body In Php

I want to get only some lines of an HTML body and I am using PHP with c URL (e.g. first 10 lines). By getting some part I mean that I don't want to download the whole file and get

Solution 1:

If the server supports it, you can make a range request.

Add, to your HTTP request headers:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Range: 0-1500',
    ));

… to get the first 1500 bytes, which may or may not be the first ten lines, depending on how long the lines are.

The problem with this (aside from possible lack of support by the server) is that you need to know which bytes in the document you want and then you have to extract the data from a partial HTML instead of a complete HTML document.

Solution 2:

You can use CURL to download partial content from a URL:

Here's a function for that:

functioncurl_get_content($url,$range_start,$range_end)
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array ("Range: bytes=$range_start-$range_end"));
$data = curl_exec($ch);
curl_close($ch);
return$data;
}


curl_get_content("http://www.example.org/some-file.zip",0,5000) 

returns the first 5000 bytes.

Notes:

Finding out if HTTP 206 is supported or not by the remote server

curl -I http://s0.cyberciti.org/images/misc/static/2012/11/ifdata-welcome-0.png

Sample outputs:

HTTP/1.0200OKContent-Type:image/pngContent-Length:36907Connection:keep-aliveServer:nginxDate:Wed,07Nov2012 00:44:47 GMTX-Whom:l3-com-cyberCache-Control:public,max-age=432000000Expires:Fri,17Jul2026 00:44:46 GMTAccept-Ranges:bytes//Itaccepts!ETag:"278099835"Last-Modified:Mon,05Nov2012 23:06:34 GMTAge:298127

Post a Comment for "How To Get Some Part Of A Html Body In Php"