/** * @param Request $request * @return Response */ public function call(Request $request) { // Create cURL $ch = curl_init(); // Set-up URL curl_setopt($ch, CURLOPT_URL, $request->getUrl()); // Set-up headers $headers = $request->getHeaders(); array_walk($headers, function (&$item, $key) { $item = "{$key}: {$item}"; }); curl_setopt($ch, CURLOPT_HTTPHEADER, array_values($headers)); // Set-up others curl_setopt_array($ch, $request->getOpts()); // Receive result $result = curl_exec($ch); // Parse response $response = new Response(); if ($result === FALSE) { $response->setError(curl_strerror(curl_errno($ch))); $response->setData(FALSE); $response->setCode(curl_errno($ch)); $response->setHeaders(curl_getinfo($ch)); } else { $response->setData(json_decode($result)); $response->setCode(curl_getinfo($ch, CURLINFO_HTTP_CODE)); $response->setHeaders(curl_getinfo($ch)); } // Close cURL curl_close($ch); return $response; }
private function ConvertCurlResponse($response, $ch) { $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $header = substr($response, 0, $header_size); $body = substr($response, $header_size); $r = new Response(); $r->setHeaders($this->http_parse_headers($header)); $r->setHttpCode(curl_getinfo($ch, CURLINFO_HTTP_CODE)); $r->setBody($body); return $r; }
public function response($code = 200, $headers = array()) { $response = new Response(); $body = json_encode(['success' => $this->success, 'data' => $this->data]); $defaultOptions = ['Content-Type' => 'application/json', 'Content-Length' => strlen($body)]; $finalHeaders = array_merge($defaultOptions, $headers); $response->setHeaders($finalHeaders); $response->setBody($body); $response->respondWith($code); return $response; }
private function doExecute($curlHandle, Request $request) { curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10); curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlHandle, CURLOPT_HEADER, true); curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array('Accept: ' . $request->getHttpAccept())); $res = curl_exec($curlHandle); $info = curl_getinfo($curlHandle); $response = new Response(); $response->setStatus($info['http_code']); if (false !== $res) { $response->setHeaders(substr($res, 0, $info['header_size'])); $response->setBody(substr($res, -$info['download_content_length'])); } return $response; }
} $oResponse->setData($result); $oResponse->flushJson(); } if (Request::getApiParam('mode') === 'addfolder') { $path = Request::getApiParam('path'); $name = Request::getApiParam('name'); $result = $oFtp->mkdir($path . '/' . $name); if (!$result) { throw new Exception("Unknown error creating this folder"); } $oResponse->setData($result); $oResponse->flushJson(); } if (Request::getApiParam('mode') === 'compress' || Request::getApiParam('mode') === 'extract') { $oResponse->setData(true); $oResponse->flushJson(); } if (Request::getQuery('mode') === 'download') { $download = Request::getQuery('preview') === 'true' ? '' : 'attachment;'; $filePath = Request::getQuery('path'); $fileName = explode('/', $filePath); $fileName = end($fileName); $tmpFilePath = $oFtp->downloadTemp($filePath); if ($fileContent = @file_get_contents($tmpFilePath)) { $oResponse->setData($fileContent); $oResponse->setHeaders(array('Content-Type' => @mime_content_type($tmpFilePath), 'Content-disposition' => sprintf('%s filename="%s"', $download, $fileName))); } $oResponse->flush(); } throw new \Exception('This action is not available in the demo');
/** * 1) Initialise the Request * 2) Grab the AssetType based on the request and initialise it * 3) Instantiate the Response class, set the headers, and then return the content * * Rap everything in a Try/Catch block for error handling * * @param Request $Request * @param array $options * * @return string * * @catch NotFoundException * @catch ErrorException */ public static function run(Request $Request, $options = array()) { try { /** * Merge in default options */ $options = array_merge(self::$defaultOptions, $options); /** * Set the header controller. Can be overwritten by the dispatcher options */ if (isset($options['headerController']) && $options['headerController'] instanceof Asset\HeaderSetter) { $headerController = $options['headerController']; } else { $headerController = new Asset\HeaderSetter(); } /** * Initialise the Request */ $Request->init(); /** * Grab the correct AssetType */ $AssetType = Asset\Registry::getClass($Request); /** * Initialise the AssetType */ $AssetType->init(); /** * Create a response */ $Response = new Response($AssetType); $Response->setHeaderController($headerController); /** * Set the headers if told to do so */ if ($options['setHeaders']) { /** * Set the headers. */ $Response->setHeaders($options['maxAge']); } /** * If the content hasn't been modified return null so only headers are sent * otherwise return the content */ return $Response->notModified ? null : $Response->render(); } catch (Asset\NotFoundException $e) { if (isset($headerController) && $headerController instanceof Asset\HeaderSetter) { $headerController->statusCode('HTTP/1.0', 404, 'Not Found'); $headerController->headerField('Status', '404 Not Found'); } return 'Not Found Error: ' . static::getErrors($e); } catch (Asset\Type\CompilationException $e) { if (isset($AssetType) && $AssetType instanceof Asset\Type) { $AssetType->cleanUpAfterError(); } return 'Compilation Error: ' . static::getErrors($e); } catch (ErrorException $e) { return 'Error: ' . static::getErrors($e); } }
}, $list); $oResponse->setData($list); $oResponse->flushJson(); } if (Request::getApiParam('mode') === 'editfile') { $oResponse->setData($oFtp->getContent(Request::getApiParam('path'))); $oResponse->flushJson(); } if (Request::getApiParam('mode') === 'addfolder') { $path = Request::getApiParam('path'); $name = Request::getApiParam('name'); $result = $oFtp->mkdir($path . '/' . $name); if (!$result) { throw new Exception("Unknown error creating this folder"); } $oResponse->setData($result); $oResponse->flushJson(); } if (Request::getQuery('mode') === 'download') { $download = Request::getQuery('preview') === 'true' ? '' : 'attachment;'; $filePath = Request::getQuery('path'); $fileName = explode('/', $filePath); $fileName = end($fileName); $tmpFilePath = $oFtp->downloadTemp($filePath); if ($fileContent = @file_get_contents($tmpFilePath)) { $oResponse->setData($fileContent); $oResponse->setHeaders(array('Content-Type' => @mime_content_type($tmpFilePath), 'Content-disposition' => "{$download} filename={$fileName}")); } $oResponse->flush(); } throw new \Exception('This action is not available in the demo');
/** * @param $str * @param array $headers * @return bool */ public function rendarString($str, $headers = array()) { $headers = array_merge(array("Content-Type" => "text/html"), $headers); if (Application::DebugMode()) { $debug = new Debug(); $debug->appendRendarDebug($str); } $response = new Response($str); $response->setHeaders($headers); $response->output(); return true; }
public function testSetHeaders() { $this->object->setHeaders(array('Location' => 'http://www.example.com/', 'Content-type' => 'text/html')); $this->assertThat($this->object->getHeaders(), $this->equalTo(array('location' => 'http://www.example.com/', 'content-type' => 'text/html'))); }