0

[EDIT: I'm trying to make an HTTP request from a PHP script to an external server.]

I'm using Example 1 from http://php.net/manual/en/function.fsockopen.php modified to:

<?php
$fp = fsockopen("m2.exosite.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET /api:v1/stack/alias?1 HTTP/1.1\r\n";
    $out .= "Host: m2.exosite.com\r\n";
    $out .= "X-Exosite-CIK: a32c85ba9dda45823be416246cf8b433baa068d7\r\n";
    $out .= "Accept: application/x-www-form-urlencoded; charset=utf-8\r\n\r\n";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>

When I run this I see at the terminal:

HTTP/1.1 200 OK
Date: Wed, 07 May 2014 13:51:40 GMT
Content-Length: 4
Connection: keep-alive
Server: nginx

<60 second pause>
1=37

However, when I watch it in wireshark, I see one HTTP packet that includes the body 1=37 get sent at the same time I see the headers in my terminal. But I only see the body in my terminal when the server closes the socket (just a tcp [FIN, ACK], no data is sent). I assume that means that the body of the response is stuck in PHP somewhere. Is there some that I need to do to get the body to be read out?

4
  • If you see it in wireshark then it's already on your machine, so it can't possibly be stuck in PHP somewhere. There is lots of software between you and the actual content, and most of it does some sort of optimization that introduces buffering. Commented May 7, 2014 at 14:46
  • Jon, by "stuck in PHP", I mean stuck on the client side. The PHP code I posted is the client not the server. Commented May 7, 2014 at 14:49
  • Well, that confuses me more than it helps. You should describe your setup in more detail. Commented May 7, 2014 at 14:50
  • I'm trying to make an HTTP request from PHP script on my desktop to a server at "m2.exosite.com". Commented May 7, 2014 at 14:54

1 Answer 1

2

That last call to fgets is blocking while it waits for a newline character, 128 characters to be read, or the default timeout to expire. The previous lines were returned because they ended in \n. Per the documentation for fgets():

Reading ends when length - 1 bytes have been read, or a newline (which is included in the return value), or an EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line.

Depending on what you're trying to do, you might:

  • send the header Connection: close so that the server closes the connection, which will make that last fgets call return.

  • call stream_set_timeout with a smaller timeout value, so that the client stops waiting earlier.

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

1 Comment

Good ansr. But there's much more to the protocol than just this e.g. chunked encoding. A better solution would be to use a proper http client, such as curl or even just file wrappers.

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.