protected function jsonResponse($data) { $response = new Response(); $response->setContentType('application/json', 'UTF-8'); $response->setJsonContent($data); return $response; }
/** * Exchange a request token for an access token. * The exchange is only succesful iff the request token has been authorized. * * Never returns, calls exit() when token is exchanged or when error is returned. */ public function accessToken() { try { $this->verifyRequest(self::TOKEN_TYPE_REQUEST); $options = array(); $ttl = $this->request->get('xoauth_token_ttl'); if ($ttl) { $options['token_ttl'] = $ttl; } $verifier = $this->request->get('oauth_verifier'); if ($verifier) { $options['verifier'] = $verifier; } $store = $this->store; $token = $store->exchangeConsumerRequestForAccessToken($this->request->getParam('oauth_token', true), $options); /** @var /Foundation/Oauth/Secrets $token */ $content = array("oauth_token" => $token->token, "oauth_token_secret" => $token->token_secret); if ($token->ttl) { $content['xoauth_token_ttl'] = $token->ttl; } $this->response->setContent(http_build_query($content)); $this->response->setStatusCode(200, ""); $this->response->setContentType("application/x-www-form-urlencoded"); } catch (OauthException $e) { $this->response->setStatusCode(401, "OAuth Verification Failed: " . $e->getMessage()); } return $this->response; }
/** * Send response to client */ public function respond($data, $status = 200) { $response = new Response(); $response->setContentType('application/json'); $response->setStatusCode($status); $data = $data instanceof SimpleResultSet ? $data->toArray() : $data; $response->setContent(json_encode($data)); $response->send(); }
public function sendResponse() { if ($this->di->has('view')) { $this->di->get('view')->disable(); } $response = new Response(); $response->setContentType('application/json', 'utf8'); $response->setJsonContent($this->getResponse()); $response->send(); }
/** * PHPWebDevelopers\Api\Common\Http\Response constructor * * @param string $content * @param int $code * @param string $status */ public function __construct($content = null, $code = null, $status = null) { parent::__construct($content, $code, $status); $request = new PhalconRequest(); //@TODO Security fail!! Please do not go with this BETA $origin = $request->getHeader("ORIGIN"); if (!empty($origin)) { parent::setHeader("Access-Control-Allow-Origin", $origin); } parent::setContentType(self::CONTENT_TYPE_JSON, self::CHARSET); parent::setHeader("Access-Control-Allow-Methods", self::ALLOW_METHODS); parent::setHeader("Access-Control-Allow-Headers", self::ALLOW_HEADERS); parent::setHeader("Access-Control-Allow-Credentials", self::ALLOW_CREDENTIALS); }
function res($req, $res) { $response = new Response(); $response->setContentType('application/json'); switch ($req) { case 'INDEX': if ($res == false) { $response->setJsonContent(array('status' => 'NOT-FOUND')); } else { $response->setJsonContent(array('status' => 'FOUND', 'data' => $res)); } break; case 'READ': if ($res == false) { $response->setJsonContent(array('status' => 'NOT-FOUND')); } else { $response->setJsonContent(array('status' => 'FOUND', 'data' => $res)); } break; default: if ($res->success() == true) { switch ($req) { case 'CREATE': $response->setStatusCode(201, 'Created'); break; case 'UPDATE': $response->setStatusCode(200, 'Update Successfully'); break; case 'DELETE': $response->setStatusCode(205, 'Delete Successfully'); break; } $response->setJsonContent(array('status' => 'OK')); } else { $response->setStatusCode(409, 'Conflict'); $errors = array(); foreach ($res->getMessages() as $message) { $errors[] = $message->getMEssage(); } $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } break; } return $response; }
} else { throw new \Exception('É necessário estar registrado no sistema para realizar requisições2.', StatusCodes::NAO_AUTORIZADO); } } else { return true; } } }); $app->setEventsManager($eventsManager); $app->error(function ($exception) { $response = new Response(); $response->setContentType('application/json', 'UTF-8'); $response->setStatusCode($exception->getCode()); $response->setHeader("Access-Control-Allow-Origin", "*"); $response->setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE"); $response->setHeader("Access-Control-Allow-Headers", "Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type"); $response->setJsonContent(['error' => $exception->getMessage()]); $response->send(); return false; }); $app->after(function () use($app) { $return = $app->getReturnedValue(); if ($return instanceof Response) { $return->send(); } else { $response = new Response(); $response->setContentType('application/json', 'UTF-8'); $response->setJsonContent($return); $response->send(); } });
public function setContentType($contentType, $charset = null) { return parent::setContentType($contentType, $charset); }
/** * Create an invoice download response. * * @param array $data */ public function download(array $data) { $filename = $data['product'] . '_' . $this->date()->month . '_' . $this->date()->year . '.pdf'; $response = new Response(); $response->setHeader('Content-Description', 'File Transfer'); $response->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"'); $response->setStatusCode(200, 'OK'); $response->setContent($this->pdf($data)); $response->setContentType('application/pdf'); return $response->send(); }
public function response(Response $response, $type, $quality = 90) { if (is_string($type) && isset(self::$typesConv[$type])) { $type = self::$typesConv[$type]; } if ($type !== self::GIF && $type !== self::PNG && $type !== self::JPEG) { throw new \InvalidArgumentException("Unsupported image type."); } if ($type) { $this->getInternalImInstance()->setimageformat(self::$types[$type]); } if ($quality) { $this->getInternalImInstance()->setcompressionquality($quality > 1 ? $quality : round($quality * 100)); } $response->setContentType(image_type_to_mime_type($type)); $response->setContent((string) $this->getInternalImInstance()); $response->send(); }
protected function ajaxResponse($content, $toJson = true) { if (!$this->request->isAjax()) { $content = json_encode(['error' => "Invalid Request"]); } elseif ($toJson) { $content = json_encode($content); } $response = new Response(); $response->setStatusCode(200, 'OK'); $response->setContentType('application/json', 'UTF-8'); $response->setContent($content); return $response; }
function api_pmail($app, $msg_id) { global $prop_tz, $server_tz, $user_id, $pm_deleted_by_receiver, $pm_deleted_by_sender; $response = new Response(); $query = 'SELECT s.username as author, ss.username as recipient, p.subject, p.id as msg_id, p.sender, p.receiver, CONVERT_TZ(p.created, \'' . $server_tz . '\', \'' . $prop_tz . ':00\') as created, p.body, s.id as sid, ss.id as rid, p.status, p.chars from confa_users s, confa_users ss, confa_pm p where s.id=p.sender and ss.id=p.receiver and p.id=' . $msg_id . ' and ' . '(p.sender=' . $user_id . ' and !(p.status & ' . $pm_deleted_by_sender . ') or p.receiver=' . $user_id . ' and !(p.status & ' . $pm_deleted_by_receiver . '))'; $result = mysql_query($query); if (!$result) { $response->setStatusCode(400, 'Error'); $response->setContentType('application/json'); $response->setJsonContent(array('status' => 'ERROR', 'messages' => array(mysql_error()))); return $response; } if (mysql_num_rows($result) != 0) { $row = mysql_fetch_assoc($result); $response->setContentType('application/json'); $response->setJsonContent(array('id' => intval($row['msg_id']), 'status' => intval($row['status']), 'subject' => api_get_subject($row['subject']), 'author' => array('id' => intval($row['sid']), 'name' => $row['author']), 'recipient' => array('id' => intval($row['rid']), 'name' => $row['recipient']), 'created' => $row['created'], 'body' => array('html' => api_get_body($row['body'])))); } else { $response->setStatusCode(404, 'Not Found')->sendHeaders(); $response->setContentType('application/json'); $response->setJsonContent(array('status' => 'ERROR', 'messages' => array('Message not found'))); } return $response; }
public function sendAjax($payload, $encode = true, array $headers = array()) { $status = 200; $description = 'OK'; $contentType = 'application/json'; $content = $encode ? json_encode($payload) : $payload; $response = new Response(); $response->setStatusCode($status, $description); $response->setContentType($contentType, 'UTF-8'); $response->setContent($content); // Set the additional headers foreach ($headers as $key => $value) { $response->setHeader($key, $value); } $this->view->disable(); return $response; }
protected function render() { $format = $this->getFormat(); $payload = $this->getPayload(); $status = $this->getStatusCode(); $description = $this->getResponseDescription($status); $headers = $this->getHeaders(); switch ($format) { case 'json': $contentType = 'application/json'; $content = json_encode($payload); break; case 'msgpack': $contentType = 'application/x-msgpack'; $content = msgpack_pack($payload); break; default: $contentType = 'text/plain'; $content = $payload; break; } $response = new Response(); $response->setStatusCode($status, $description); $response->setContentType($contentType, 'UTF-8'); // $response->setHeader('Access-Control-Allow-Origin', '*'); $response->setHeader('Access-Control-Allow-Headers', 'X-Requested-With'); $response->setContent($content); // Set the additional headers foreach ($headers as $key => $value) { $response->setHeader($key, $value); } $this->view->disable(); return $response; }