Where the headers come from when just calling echo in PHP? -


i see come code php manual

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30); if (!$fp) {     echo "$errstr ($errno)<br />\n"; } else {     $out = "get / http/1.1\r\n";     $out .= "host: www.example.com\r\n";     $out .= "connection: close\r\n\r\n";     fwrite($fp, $out);     while (!feof($fp)) {         echo fgets($fp, 128);     }     fclose($fp); } 

writing header echo, going work?

when calling simple code this:

echo 'hello'; 

where http headers from?

what you're doing in code is:

  1. open socket connection remote http server (www.example.com on port 80). establishes tcp connection port.
  2. you send (via fwrite) http request on connection. http protocol on top of tcp, , you're manually formulating http protocol headers here.
  3. you're reading (via fgets) (http) response of remote server.

i'm guessing want know why see http headers in remote response, though you're doing echo 'hello'; on remote server. answer because web server running on server handling http transaction. you're not handling details of incoming http request in php, , you're not handling details of outgoing response. web server on php running (likely apache) doing that.

the whole stack includes tcp connection, carries http request, consists of http headers , http body. on server, tcp connection typically handled underlying operating system, makes connection available socket web server, web server "unwraps" http request handle , invoke php if necessary, , whole chain goes backwards response.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -