1

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

2 Answers 2

3

At the moment you're iterating over each line of the header/response even though you're only interested in the first line. Try this instead:

while (!feof($fp)) {
    $response = fgets($fp, 1024);
    $code = substr($response,9,3);
    if (is_numeric($code)) {
        $break;
     }
}

// $code should contain the response code

If you're only interested in the headers and not the response I'd suggest that you make a HEAD request instead of a GET one.

Unless there's a really compelling reason not to I'd suggest that you make this request using CURL rather than trying to handle all of the low level stuff within your PHP app logic.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I have looked into the curl option a few times, but as the code is to be used in basically 'warming' up a server ready for release to a LOT of hits/sec ... the opening of sockets seems to make the load balancers work better from the start along with other things. And I DO need to get the full content of the page, otherwise not all the servlets will be initialised, meaning a crash as soon as we let the public in :)
OK - That's surprising. It's hard to see any scientific reason as to why CURL/raw sockets would work differently. The request via CURL will also have to create a socket, write out the request to it and then read the response in exactly the same manner.
1

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));

1 Comment

You are absolutely right, bozo thinking on my part :) It IS important that the full page is fetched like a browser would fetch it in order for it to be compiled properly, thus I was thinking I needed to process the entire file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.