Example #1
0
 public function executeRequest(Request $request)
 {
     // create request
     // 80 is the port of the web server
     $connection = fsockopen($request->getHost(), 80);
     $requestString = $this->getRequestStringRepresentation($request);
     fwrite($connection, $requestString);
     // get response
     // first line == status line
     $result = '';
     $statusLine = '';
     while ($line = fgets($connection)) {
         if (empty($statusLine)) {
             $statusLine = $line;
             continue;
         }
         $result .= $line;
     }
     // close connection
     fclose($connection);
     // split result into header and content
     // the header ends with \r\n\r\n
     $this->response = new ResponseImpl();
     $this->setHeadersFromString($this->response, $this->getHeadersString($result));
     $this->response->setBody($this->getBodyString($result));
     // parse status line
     // status-line-pattern: <HTTP-Version> <status-code> <reason-phrase>
     $status = explode(' ', $statusLine);
     $this->response->setVersion($status[0]);
     $this->response->setStatusCode($status[1]);
     $this->response->setReasonPhrase($status[2]);
     return $this->response;
 }