SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Streaming Remote file (progressive)

    Hi,
    im trying to stream a remote file (url). the problem is php doesnt echo the contents immediately but waits for few seconds , ive done this with readfile() and with readfile() its starts buffering immediatly (using flush()) but it waits for few sec with curl, fopen and fsockopen. im wana use these function cuz i want to use a proxy server to cache contents
    The following code works perfect(starts outputting immediatly)
    PHP Code:
    <?php
    readfile
    ('http://myurl/myflie.txt');
    flush();
    ?>
    But this wont:
    PHP Code:
    <?php
    ob_flush
    ();
    $proxy "85.223.178.230";
    $port 80;
    /
    $url "http://myurl/myfile";
    $fp fsockopen($proxy$port);
    fputs($fp"GET $url HTTP/1.0\r\nHost: $proxy\r\n\r\n");
    while(!
    feof($fp)){
    print(
    fread($fp102400));
    flush();
    }
    fclose($fp);

    ?>
    also tried curl with proxy but it also waits for some sec

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Could it be that it takes those few seconds to connect to the proxy which might have a slower connection that the direct url?
    Saul

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nah PHP downloloads the whole file in memory then starts outputting while the readfile output the file as data is downloaded

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem could be:
    Code:
    print(fread($fp, 102400));
    ~100 KB (102400) certainly takes a while to read via proxy.
    Try replacing it with smth <1400, so that you output the data arriving with every TCP packet received.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah i also tried 1024 but still same , anyway ive found an alternative , but now the problem is that when outputting the data the headers are also printed to the output anyway for removing them?

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2007
    Posts
    31
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're getting headers output as you're doing the connection on the tcp socket level. The read() function does not 'know' anything about the http protocol, that's why the headers are being output.
    The easy way to strip those is - discard everything you're getting from the remote server untill you get \r\n\r\n - the empty line from http server is marking end of headers and beginning of the content. I.e. - you'll need to gather the output from the remove server into the temporary b uffer. Either line-by-line, which is going to be easier, as you will not need to find the offset of \r\n\r\n in the buffer, or block-by block, then you'll need to drop everything you recieve up to the position of \r\n\r\n.

    Another approach (which is imho way more correct) is to use CURL curl_exec function, which is returning the fetched content directly to the user's browser.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •