Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7811579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:12:59+00:00 2026-06-02T04:12:59+00:00

I am running into a silly problem: I need to get a certain number

  • 0

I am running into a silly problem: I need to get a certain number of bytes, then close the connection. I’ve found I can extract the “Content Length” from the header, and use the length to determine just how many more bytes I should get before severing the connection. However, it appears I have made a mistake. It is only getting 40 bytes, instead of 2428 (as it should). Perhaps I am not using fgets() correctly?

Here is the code:

private static function Send($URL, $Data) {
            $server = parse_url($URL, PHP_URL_HOST);
            $port = parse_url($URL, PHP_URL_PORT);

            // If the parsing the port information fails, we will assume it's on a default port.
            // As such, we'll set the port in the switch below.
            if($port == null) {
                switch(parse_url($URL, PHP_URL_SCHEME)) {
                    case "HTTP":
                        $port = 80;
                        break;
                    case "HTTPS":
                        $port = 443;
                        break;

                }
            }

            // Check if we are using a proxy (debug configuration typically).
            if(\HTTP\HTTPRequests::ProxyEnabled) {
                $server = \HTTP\HTTPRequests::ProxyServer;
                $port = \HTTP\HTTPRequests::ProxyPort;
            }



            // Open a connection to the server.
            $connection = fsockopen($server, $port, $errno, $errstr);
            if (!$connection) {
                die("OMG Ponies!");
            }
            echo "===========================================================<BR>";
            echo "Connection Open<BR>";
            echo "The Time is " . date("H:i:su", time()) . "<BR>";
            echo "Sending Request<BR>";

            fwrite($connection, $Data);

            echo "Request Sent.";
            echo "The Time is " . date("H:i:su", time()) . "<BR>";
            $responseheader = "";
            $responsebody = "";


            /*
            \HTTP\HTTPRequests::$start = NULL;
            \HTTP\HTTPRequests::$timeout = 10;

            // @todo: Rewrite this. Should keep checking for '/r/n/r/n', then check for a content length header. If found, keep grabbing bytes, then close. If not, then close immediately.
            while(!\HTTP\HTTPRequests::safe_feof($connection, \HTTP\HTTPRequests::$start) && (microtime(true) - \HTTP\HTTPRequests::$start) < \HTTP\HTTPRequests::$timeout)
            {
                $response .= fgets($connection);
            }
            */
            echo "Getting Response<BR>";
            echo "The Time is " . date("H:i:su", time()) . "<BR>";
            while(!feof($connection) && !(strlen(strstr($responseheader,"\r\n\r\n"))>0)) {
                $responseheader .= fgets($connection);
            }

            echo "The Header is fully received at " . date("H:i:su", time()) . "<BR>";
            echo "Header (raw):" . "<BR>";
            echo "/////////////////////////////////////////////<BR>";
            echo $responseheader . "<BR>";
            echo "/////////////////////////////////////////////<BR>";

            if((strlen(strstr($responseheader,"Content-Length:"))>0)) {
                $contentlength = ((int)(\Extract\Extract::GetContentLength($responseheader)));
                //for($i = 0; $i < $contentlength; $i++) {
                    $responsebody .= fgets($connection, $contentlength);
                //}
                echo "The Body is fully received at " . date("H:i:su", time()) . "<BR>";
                echo "Body (raw):" . "<BR>";
                echo "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}<BR>";
                echo $responsebody . "<BR>";
                echo "{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}<BR>";
                echo "<B>!Content length is " . strlen($responsebody) . ". It should be " . $contentlength . "!</B><BR>";
            }

            echo "Response Received.<BR>";
            echo "The Time is " . date("H:i:su", time()) . "<BR>";
            fclose($connection);

            echo "Connection Closed.<BR>";
            echo "The Time is " . date("H:i:su", time()) . "<BR>";
            echo "Exiting Send() method.<BR>";
            echo "===========================================================<BR>";
            echo "<BR>";

            return $responseheader . $responsebody;
        }

And here is the output:

===========================================================
Connection Open
The Time is 15:57:29000000
Sending Request
Request Sent.The Time is 15:57:29000000
Getting Response
The Time is 15:57:29000000
The Header is fully received at 15:57:29000000
Header (raw):
/////////////////////////////////////////////
HTTP/1.1 207 Multi-Status Date: Wed, 11 Apr 2012 19:57:18 GMT Server: Apache/2.2.8 (Ubuntu) PHP/5.2.4-2ubuntu5.14 with Suhosin-Patch mod_ssl/2.2.8 OpenSSL/0.9.8g mod_wsgi/2.0 Python/2.5.2 mod_perl/2.0.3 Perl/v5.8.8 X-Powered-By: PHP/5.2.4-2ubuntu5.14 DAV: 1, 2, 3, access-control, calendar-access, calendar-schedule DAV: extended-mkcol, calendar-proxy, bind, addressbook, calendar-auto-schedule Content-Location: /davical/caldav.php/rwr26/home/ ETag: "8378ead7e628deafd91ec99dc180ad74" X-DAViCal-Version: DAViCal/0.9.9; DB/1.2.9 Content-Length: 2428 Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Content-Type: text/xml; charset="utf-8" 
/////////////////////////////////////////////
The Body is fully received at 15:57:29000000
Body (raw):
{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}

{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}
!Content length is 40. It should be 2428!
Response Received.
The Time is 15:57:29000000
Connection Closed.
The Time is 15:57:29000000
Exiting Send() method.
===========================================================

Why am I doing it this way? Apparently, I was running into some sort of a timeout error, which makes each connection stay open for 30 seconds (yes, 30 whole seconds). So I wrote this code to get the exact number of bytes, then close. Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:13:00+00:00Added an answer on June 2, 2026 at 4:13 am

    I changed the content line “$responsebody .= fgets($connection, $contentlength);” to “$responsebody .= stream_get_line($connection, $contentlength);”. Thing is wicked fast now, and works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Running into a problem where on certain servers we get an error that the
This may seem like a silly title but I'm running into a small problem
Running into a problem. I have a table defined to hold the values of
I'm running into a problem when trying to select records from my 2005 MS-SQL
I'm running into an odd issue retrieving printer port addresses. When I get all
Im running into this error that I can't work out Im writing some code
i'm running into a strange problem in Microsoft SQL Server 2008. I have a
I'm running into a bit of a problem with my WCF service that is
I'm running into a problem using the jQuery Uploadify plugin in my cakePHP application.
I'm running into a problem starting other activities from mine. I know this must

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.