Exemple #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;
 }
Exemple #2
0
 /**
  * @param Request $request The request to serialize.
  *
  * @return string The string representation of the given request.
  */
 public function getRequestStringRepresentation(Request $request)
 {
     // create query string
     $url = $request->getUrl();
     $queryString = http_build_query($url->getQuery());
     if (!empty($queryString)) {
         $queryString = '?' . $queryString;
     }
     $rawUrlPath = $url->getPath();
     $urlPath = $rawUrlPath == '' || $rawUrlPath == null ? '/' : $rawUrlPath;
     // create request string presentation
     $requestString = $request->getMethod() . ' ' . $urlPath . $queryString . ' HTTP/' . $request->getVersion() . "\r\n";
     $requestString .= 'Host: ' . $request->getHost() . "\r\n";
     foreach ($request->getHeaders() as $header) {
         $requestString .= $header->getName() . ': ' . $header->getValue() . "\r\n";
     }
     if ($request->getMethod() === Request::METHOD_POST) {
         $query = http_build_query($request->getParameters());
         $contentLength = strlen($query);
         $requestString .= Header::CONTENT_TYPE . ': application/x-www-form-urlencoded' . "\r\n";
         $requestString .= Header::CONTENT_LENGTH . ': ' . $contentLength . "\r\n\r\n";
         $requestString .= $query;
         return $requestString;
     }
     return $requestString . "\r\n";
 }
Exemple #3
0
 public function executeRequest(Request $request)
 {
     $cH = curl_init($request->getUrl());
     curl_setopt($cH, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($cH, CURLOPT_HEADER, true);
     if ($request->getMethod() === Request::METHOD_POST) {
         curl_setopt($cH, CURLOPT_POST, true);
         curl_setopt($cH, CURLOPT_POSTFIELDS, http_build_query($request->getParameters()));
     }
     $result = curl_exec($cH);
     $this->response = new ResponseImpl();
     $this->response->setStatusCode(curl_getinfo($cH, CURLINFO_HTTP_CODE));
     $this->setHeadersFromString($this->response, $this->getHeadersString($result));
     $this->response->setBody($this->getBodyString($result));
     curl_close($cH);
     return $this->response;
 }