/** * @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"; }
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; }