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 7531229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:07:04+00:00 2026-05-30T05:07:04+00:00

I have a script set up that for reasons of necessity gets both the

  • 0

I have a script set up that for reasons of necessity gets both the HTTP Response header and then content of a GET request using fsock.

function checkUrl($host,$url,$port) {
$fp = fsockopen($host, $port, $errno, $errstr, 10);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET $url HTTP/1.1\r\n";
    $out .= "Host: $host\r\n";
    $out .= "Connection: Close\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        $response = fgets($fp, 1024);
        print(substr($response,9,3));
    }
    fclose($fp);
}
}

I call it and get all the proper data back if I simply echo it all out. But actually all I need to return from the function is the HTTP STATUS Code.

i.e. 404 or 200 or 301 etc

But the code above gives the error code sure, but then with a load of gibberish at the end which I don’t understand when I have limited to 3 chars!

e.g.

404, 2BM_n: Encype HThe tp-me=srcsrclanstaPre> lanmg=[0][1][2][3][4][5][6][7][8][9][10[11[12 swt.i> ypeeleamiize#99eco#66ade#33izeine#CCize { #66izeeig tmardespath=th=th=th=th=th=th=spardeolordeignign bocol widwidwid col bler> td Sorabl> e> rdeolordespath=th=th= bo spardeoloe="lanSen>

Which leads me to believe that my response is actually more complex than just a string right? Something special with the header or am I misunderstanding how fgets is working? Any help much 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-05-30T05:07:06+00:00Added an answer on May 30, 2026 at 5:07 am

    The problem is you are printing out that substring for every block of 1024 characters instead of just the first. The solution is to not do the loop. Change this:

    while (!feof($fp)) {
        $response = fgets($fp, 1024);
        print(substr($response,9,3));
    }
    

    To just this:

    $response = fgets($fp, 1024);
    print(substr($response,9,3));
    

    Or even just this, really, since you only need the first 13 characters, not the first 1024:

    $response = fgets($fp, 13);
    print(substr($response,9,3));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script that handles a form input. For design reasons both
I have a bash script set that executes in a folder containing a number
I have this script set up that echoes all relevant users in a database
I have a bash script that has set -x in it. Is it possible
I have a script that starts Tomcat and it looks like this: rem set
First, the set up: I have a script that executes several tasks after a
I have a ruby script that opens a browser. I have it set to
I have a CRON job php script that I just set up not too
I'm writing a bash script and I have errexit set, so that the script
I have a script that removes a class for functional reasons, the problem is

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.