Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2010
    Posts
    302
    Thanks
    68
    Thanked 8 Times in 8 Posts

    How to get number of bytes sent by server in response to a specific request?

    The PECL function Memcache::getStats returns, amongst other things, the "Total number of bytes sent by this server to network ". (i.e. the whole lot, not just what was sent in response to a particular request).

    BUT ... is there a pure PHP way of finding out how many bytes were sent in response to a particular request? I know that apache's access.log file contains this information, but I was hoping to find a pure PHP solution for the sake of portability.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,476
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Well yes. Write your own socket code that communicates via port 80 to make a standard http request and then count the bytes that you get back in return. This will be raw http data so will include the headers.

    This is a simple looking bit of kit from php.nets fsockopen page:

    PHP Code:

    <?php
    $fp 
    fsockopen("www.example.com"80$errno$errstr30);
    if (!
    $fp) {
        echo 
    "$errstr ($errno)<br />\n";
    } else {
        
    $out "GET / HTTP/1.1\r\n";
        
    $out .= "Host: www.example.com\r\n";
        
    $out .= "Connection: Close\r\n\r\n";
        
    fwrite($fp$out);

        while (!
    feof($fp)) {
            echo 
    fgets($fp128);
        }
        
    fclose($fp);
    }
    ?>
    Inside that while loop is where you could count your bytes.
    I can't really think of anything to write here now...

  • #3
    Regular Coder
    Join Date
    Jun 2010
    Posts
    302
    Thanks
    68
    Thanked 8 Times in 8 Posts
    Hi, thanks for the reply, I don't think I made myself clear; what I want to be able to do is to get the number of bytes sent in response to an external request (i.e. where I have no control over the client). the only way I know how to do this at present is to parse the apache access log.

  • #4

  • #5
    Regular Coder
    Join Date
    Jun 2010
    Posts
    302
    Thanks
    68
    Thanked 8 Times in 8 Posts
    Thanks, that might help me. I think what I am trying to do is not possible without the log file though.


  •  

    Posting Permissions

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