3

i'm using code like this:

//section with the important stuff for the client
ob_start();
echo "Blah... Random Content" . rand(1,1000);
$size = ob_get_length();
header("Content-Length: $size");
header('Connection: close');
ob_end_flush();
ob_flush();
flush();
//all the following output/script running time should be ignored by the client (file_get_contents())
sleep(10);
echo "long action completed";

to output some content and subsequently running a time consuming background job.
In a other file i'm trying to access the data of the first script without having to wait for the background job to finish.
Unfortunately this here doesn't work for me:

$content = file_get_contents("http://some-address/thescript.php");
echo $content;

as it doesn't pay attention to the Content-length header. In the browser the whole thing works fine though. Any suggestions? Thanks.

Stefan
  • 2,164
  • 1
  • 23
  • 40
  • not finished yet. but technically the same. I'm just trying to find a way to tell PHP to pay attention to the content-length header – Stefan Jun 07 '12 at 23:24
  • 3
    Try cURL then, or add the $length parameter to f_g_c(). – mario Jun 07 '12 at 23:25
  • 1
    doctor my elbow hurts, well technically, its not my elbow, but fix what ever it is anyway - some people are funny. –  Jun 07 '12 at 23:26
  • I've considered the use of cURL before but unfortunately it's not available on every system. Adding the $length parameter to f_g_c() doesn't work as the content length changes dynamically. – Stefan Jun 07 '12 at 23:27
  • @Dagon the only thing that will be changed later is the content (it'll be fetched from a db) but i really don't think that would be important here – Stefan Jun 07 '12 at 23:32
  • This works fine with curl. Not sure why you'd want / expect `file_get_contents` to respond to headers. If you're really stuck, use a process fork to carry out the background op. – lsl Jun 08 '12 at 00:13
  • i got it working with file_get_contents(), no need to fork it – Stefan Jun 08 '12 at 00:17
  • @Stefan - "I've considered the use of cURL before but unfortunately it's not available on every system." Well, that's not quite true. Here is a [very complete cURL emulation layer](http://barebonescms.com/documentation/ultimate_web_scraper_toolkit/). – CubicleSoft Jun 08 '12 at 03:10
  • i'll have a look at that, thanks – Stefan Jun 08 '12 at 17:24

1 Answers1

2

Fixed it. In case anyone has the same problem:


$url = "http://some-adress/test.php";
$headers = get_headers($url, 1);
$content_length = $headers["Content-Length"];
$content = file_get_contents($url, NULL, NULL, NULL, $content_length);
echo $content;


Stefan
  • 2,164
  • 1
  • 23
  • 40