/** * @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; }
/** * @param Request $req * @param Response $res * * @return Response */ public function __invoke($req, $res) { if ($req->isHtml()) { $res->render(new View('not_found', ['title' => 'Not Found'])); } return $res->setCode(404); }
/** * @param Request $req * @param Response $res * @param array $allowedMethods * * @return Response */ public function __invoke($req, $res, $allowedMethods) { if ($req->isHtml()) { $res->render(new View('method_not_allowed', ['title' => 'Method Not Allowed', 'allowedMethods' => $allowedMethods])); } return $res->setCode(405); }
/** * @param Request $req * @param Response $res * @param \Exception $e * * @return Response */ public function __invoke($req, $res, \Exception $e) { $this->app['logger']->error('An uncaught exception occurred while handling a request.', ['exception' => $e]); if ($req->isHtml()) { $res->render(new View('exception', ['title' => 'Internal Server Error', 'exception' => $e])); } return $res->setCode(500); }
/** * {@inheritdoc} * @see \InoOicClient\Oic\Authorization\ResponseFactoryInterface::createResponse() */ public function createResponse($code, $state = null) { $response = new Response(); $response->setCode($code); if (null !== $state) { $response->setState($state); } return $response; }
/** * * Report the error * * @param [string] The message to be displayed * @param [boolean] Whether or not to display PHP backtrace */ function report($msg, $backtrace = true) { // Check for error reporting flag if ($this->config['error_reporting']) { Template::load('error_reporting.html', array("msg" => $msg, "backtrace" => print_r(debug_backtrace(), true))); exit; } else { // Prepare 404 response $Response = new Response(); $Response->setCode(404); $Response->send(); Template::load('404.html'); } exit; }
/** * @route POST /?method=users.create&format=json * @route POST /users/create.json * * @param Request $request * @return Response * @throws Exception */ public function createAction($request) { $request->acceptContentTypes(array('json')); if ('POST' != $request->getMethod()) { throw new Exception('HTTP method not allowed', Response::NOT_ALLOWED); } try { $user = new User(array('name' => $request->getPost('name'), 'username' => $request->getPost('username'), 'email' => $request->getPost('email'), 'gender' => $request->getPost('gender'))); } catch (ValidationException $e) { throw new Exception($e->getMessage(), Response::OK); } $id = $this->getModel('User')->save($user); if (!is_numeric($id)) { throw new Exception('An error occurred while creating user', Response::OK); } $response = new Response(); $response->setCode(Response::CREATED); $response->setEtagHeader(md5('/users/' . $id)); return $response; }
public static function runApp() { if (!Request::isАuthorized()) { Response::setCode(401); return; } $matchedResource = Router::getMatchedRouterResource(Request::getMethod(), Request::getCleanRequestUrl()); if ($matchedResource === null) { Response::setCode(404); return; } try { $response = self::executeResoruceAction($matchedResource[0], $matchedResource[1], Router::getMachedRouteParameters()); if ($response === false) { Response::setCode(404); } else { Response::setBody($response); } } catch (\Exception $e) { Response::setCode(500); } }
private function buildTransferReversal($xml) { $transferReversal = new TransferReversal(); $transferReversal->setRequestId((string) $xml->RequestId); $transferReversal->setOriginalRequestId((string) $xml->OriginalRequestId); $transferReversal->setTransactionReference((string) $xml->TransactionReference); $transactionHistory = new TransactionHistory(); $tmpTransaction = new Transaction(); $transaction = $xml->TransactionHistory->Transaction; $tmpTransaction->setType((string) $transaction->Type); $tmpTransaction->setSystemTraceAuditNumber((string) $transaction->SystemTraceAuditNumber); $tmpTransaction->setNetworkReferenceNumber((string) $transaction->NetworkReferenceNumber); $tmpTransaction->setSettlementDate((string) $transaction->SettlementDate); $tmpResponse = new Response(); $response = $transaction->Response; $tmpResponse->setCode((string) $response->Code); $tmpResponse->setDescription((string) $response->Description); $tmpTransaction->setSubmitDateTime((string) $transaction->SubmitDateTime); $tmpTransaction->setResponse($tmpResponse); $transactionHistory->setTransaction($tmpTransaction); $transferReversal->setTransactionHistory($transactionHistory); return $transferReversal; }
/** * @param string $uri * @param integer $code HTTP status codes * @return void */ public function redirect($uri, $code = Response::FOUND) { $response = new Response(); $response->setCode($code); $response->addHeader('Location: ' . $uri); $response->sendHeaders(); exit; }
private function buildTransfer($xml) { $transfer = new Transfer(); $transfer->setRequestId((string) $xml->RequestId); $transfer->setTransactionReference((string) $xml->TransactionReference); $transactionHistory = new TransactionHistory(); $transactionArray = array(); foreach ($xml->TransactionHistory->Transaction as $transaction) { $tmpTransaction = new Transaction(); $tmpTransaction->setType((string) $transaction->Type); $tmpTransaction->setSystemTraceAuditNumber((string) $transaction->SystemTraceAuditNumber); $tmpTransaction->setNetworkReferenceNumber((string) $transaction->NetworkReferenceNumber); $tmpTransaction->setSettlementDate((string) $transaction->SettlementDate); $tmpResponse = new Response(); $response = $transaction->Response; $tmpResponse->setCode((string) $response->Code); $tmpResponse->setDescription((string) $response->Description); $tmpTransaction->setSubmitDateTime((string) $transaction->SubmitDateTime); $tmpTransaction->setResponse($tmpResponse); array_push($transactionArray, $tmpTransaction); } $transactionHistory->setTransaction($transactionArray); $transfer->setTransactionHistory($transactionHistory); return $transfer; }
public function setResponseCode($code) { Response::setCode($code); }