Strange behavior when using sockets
<p>I'm getting this strange behavior when fetching a website using socket. The string returned from get_content() function below include some "extra information" that are not present on the original website.</p>

<pre><code>function get_content($a, $b, $c = "00")
{
    $request  = "arg01="  . $a;
    $request .= "&amp;arg02="  . $b;
    $request .= "&amp;arg03="  . $c;

    $host = "www.site.com";
    $script = "/page.php";
    $method = "POST";

    $request_length = strlen($request);

    $header = "$method $script HTTP/1.1\r\n";
    $header .= "Host: $host\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Content-Length: $request_length\r\n";
    $header .= "Connection: close\r\n\r\n";
    $header .= "$request\r\n";

    $socket = @fsockopen($host, 80, $errno, $errstr);
    if ($socket) {
        fputs($socket, $header);
            while(!feof($socket)) {
                $output .= fgets($socket);
            }
        fclose($socket);
    }

    return $output;
}
</code></pre>

<p>Printing $output:</p>

<pre><code>HTTP/1.1 200 OK
Date: Fri, 24 Jul 2009 15:20:38 GMT
Server: Apache/2.2.8 (Unix) PHP/4.4.8
X-Powered-By: PHP/4.4.8
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

1f61
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;

    (...) html here (...)
            &lt;td align='right'&gt;&lt;font size='-1'&gt;   18,65&lt;/font&gt;&lt;/td&gt;
    (...) html here (...)
            &lt;td align='right'&gt;&lt;font size='-1'&gt;   24,10&lt;/font&gt;&lt;/td&gt;
    (...) html here (...)
            &lt;td align='right'&gt;&lt;font size='-1'&gt;   18,40&lt;/font&gt;&lt;/td&gt;
    (...) html here (...)
            &lt;td align='right'&gt;&lt;font size='-1'&gt;   24,10&lt;/font&gt;&lt;/td&gt;
    (...) html here (...)
            &lt;td align='right'&gt;&lt;font size='-1'&gt;   24,10&lt;/font&gt;&lt;/td&gt;
    (...) html here (...)
            &lt;td align='right'&gt;&lt;font size='-1'&gt;   18,65&lt;/font&gt;&lt;/td&gt;
    (...) html here (...)
            &lt;td align='right'&gt;&lt;font size='-1'&gt;
f43
   24,10&lt;/font&gt;&lt;/td&gt;
    (...) html here (...)
            &lt;td align='right'&gt;&lt;font size='-1'&gt;   18,65&lt;/font&gt;&lt;/td&gt;
    (...) html here (...)
            &lt;td align='right'&gt;&lt;font size='-1'&gt;   18,65&lt;/font&gt;&lt;/td&gt;
    (...) html here (...)
            &lt;td align='right'&gt;&lt;font size='-1'&gt;   24,10&lt;/font&gt;&lt;/td&gt;
    (...) html here (...)
            &lt;td align='right'&gt;&lt;font size='-1'&gt;   18,40&lt;/font&gt;&lt;/td&gt;

    (...) html here (...)
   &lt;/body&gt;
&lt;/html&gt;

0
</code></pre>

<p>Note the block below:</p>

<pre><code>            &lt;td align='right'&gt;&lt;font size='-1'&gt;
f43
   24,10&lt;/font&gt;&lt;/td&gt;
</code></pre>

<p>It is not present on the original HTML. Should be</p>

<pre><code>            &lt;td align='right'&gt;&lt;font size='-1'&gt;   24,10&lt;/font&gt;&lt;/td&gt;
</code></pre>

<p>just like on others td tags.</p>

<p>To fix this issue, I replaced sockets by curl.</p>

<pre><code>function get_content($a, $b, $c = "00")
{
    $args  = "arg01="  . $a;
    $args .= "&amp;arg02="  . $b;
    $args .= "&amp;arg03="  . $c;

    $host = "http://www.site.com/page.php";

    $ch = curl_init($host);
    curl_setopt($ch, CURLOPT_URL, $host);
    curl_setopt($ch, CURLOPT_POST, count($args));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_exec($ch);
    $output = curl_multi_getcontent($ch);
    curl_close($ch);

    return $output;
}
</code></pre>

<p>Although CURL fixes the problem, I'd like to understand what is happening here. What is the origin of this "extra information"? From a wormhole or something like that?</p>

<p>Do you have any thoughts?</p>

<p>Thanks!</p>
