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
    293
    Thanks
    63
    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,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Regular Coder
    Join Date
    Jun 2010
    Posts
    293
    Thanks
    63
    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
    Regular Coder
    Join Date
    Sep 2011
    Posts
    410
    Thanks
    18
    Thanked 26 Times in 26 Posts
    If I've helped you out, show your appreciation by clicking the "Thanks" link as well as a link below!

    AdFly
    Facebook | Twitter
    Google | YouTube

  • #5
    Regular Coder
    Join Date
    Jun 2010
    Posts
    293
    Thanks
    63
    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
    •