public static function Json($json) { $response = new \Phalcon\Http\Response(); $response->setJsonContent($json); $contentLength = strlen($response->getContent()); $response->setHeader('ContentLength', $contentLength); $response->send(); die; }
public static function p($status = 1, $msg = null, $data = [], $code = 200) { if (method_exists($msg, 'getMessage')) { $msg = $msg->getMessage(); } $response = new \Phalcon\Http\Response(); $response->setHeader("Content-Type", "application/json"); if ($status) { $response->setStatusCode(200); } else { $response->setStatusCode($code); } $response->setJsonContent(array('status' => $status == 1 ? 'success' : 'error', 'msg' => $msg, 'data' => $data)); $response->send(); }
public function getProductAction($categoryid) { $catid = intval($categoryid); if ($catid == 0) { $products = Product::find(array('order' => 'productid asc')); } else { $products = Product::find(array('conditions' => 'catid = ?0', 'bind' => array(0 => $catid), 'order' => 'productid asc')); } $response = new \Phalcon\Http\Response(); $response->setHeader("Access-Control-Allow-Origin: *"); $response->setHeader("Content-Type", "application/json"); $response->setJsonContent($products->toArray()); $this->view->disable(); return $response; }
public static function responseJson(array $data = array(), $state = 200) { $datetime = gmdate("D, d M Y H:i:s") . ' GMT'; $response = new \Phalcon\Http\Response(); $response->setHeader('Pragma', 'no-cache'); $response->setHeader('Cache-Control', 'no-cache, private, no-store, must-revalidate, pre-check=0, post-check=0, max-age=0, max-stale=0'); $response->setHeader('Last-Modified', $datetime); $response->setHeader('X-Frame-Options', 'SAMEORIGIN'); $response->setContentType('application/json', 'UTF-8'); $response->setExpires(new \DateTime()); $response->setStatusCode($state); $response->setEtag(md5($datetime)); $response->setJsonContent($data, JSON_UNESCAPED_UNICODE); $response->send(); // Force quit the dispatcher loop. exit; }
/** * Create a Response object with related parameters * * @param string $content * @param int $status * @param string $contentType * @param null $message * @return \Phalcon\Http\Response */ protected function createResponse($content = self::NO_CONTENT, $status = self::STATUS_OK, $contentType = self::JSON, $message = null) { $response = new \Phalcon\Http\Response(); // Allow CORS $response->setHeader(self::HEADER_ALLOW_ORIGIN, "*"); // set new token to protected api if (!in_array($this->dispatcher->getActionName(), $this->publicAction) && !in_array($this->dispatcher->getActionName(), $this->authErrAction)) { $response->setHeader(self::HEADER_ACCESS_TOKEN, $this->session->get("loggedInUser")->access_token); } if ($message) { $response->setStatusCode($status, $message); } else { $response->setStatusCode($status, self::$MESSAGES[$status]); } if ($content !== self::NO_CONTENT) { $response->setHeader(self::HEADER_CONTENT_TYPE, $contentType); if ($contentType == BaseRestApiController::JSON) { $response->setJsonContent($content); } else { $response->setContent($content); } } return $response; }
protected function respondWithStatusCode($code = HttpStatusCode::OK, $message = null) { $this->view->disable(); $response = new \Phalcon\Http\Response(); $response->setStatusCode($code, HttpStatusCode::getMessage($code)); if ($this->request->isAjax() || $this->clientAcceptsJson()) { $data = new \stdClass(); $data->message = $message; $response->setContentType("application/json", "utf-8"); $response->setJsonContent($data, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK); } else { $response->setContent($message); } return $response; }
$onlytestnet = ""; } $sql = sprintf("UPDATE cmd_nodes SET VersionID=%d WHERE KeepUpToDate=1{$onlytestnet}", $versionid); if ($result = $mysqli->query($sql)) { $info2 = $mysqli->info; if (is_null($info2)) { $info2 = true; } } else { $info2 = $mysqli->errno . ': ' . $mysqli->error; } //Change the HTTP status $response->setStatusCode(202, "Accepted"); $response->setJsonContent(array('status' => 'OK', 'data' => array('VersionId' => $versionid, "KeepUpToDate" => $info2))); } else { $response->setStatusCode(503, "Service Unavailable"); $response->setJsonContent(array('status' => 'ERROR', 'messages' => array($mysqli->errno . ': ' . $mysqli->error))); } } return $response; }); // ============================================================================ // End-point not found // ============================================================================ $app->notFound(function () use($app) { $response = new Phalcon\Http\Response(); $response->setStatusCode(404, "Not Found"); $response->setJsonContent(array('status' => 'ERROR', 'messages' => array('Unknown end-point'))); $response->send(); }); $app->handle();
$response->setStatusCode(500, "Internal Error"); $errors = array(); foreach ($status->getMessages() as $message) { $errors[] = $message->getMessage(); } $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; }); //eliminamos un entidadcursosala por su id $application->delete('/api/entidadcursosalas/{id:[0-9]+}', function ($id) use($application) { //creamos la consulta con phql $phql = "DELETE FROM Entidadcursosala WHERE id = :id:"; $status = $application->modelsManager->executeQuery($phql, array('id' => $id)); //creamos una respuesta $response = new Phalcon\Http\Response(); //comprobamos si la eliminación se ha llevado a cabo correctamente if ($status->success() == true) { $response->setJsonContent(array('status' => 'OK')); } else { ////en otro caso cambiamos el estado http por un 500 $response->setStatusCode(500, "Internal Error"); $errors = array(); //mostramos los errores foreach ($status->getMessages() as $message) { $errors[] = $message->getMessage(); } $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; });
} $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; }); // Send message to a selected user. $app->post('/chat/{sender}/{receiver}', function ($sender, $receiver) use($app) { $message = $app->request->getJsonRawBody(); $phql = "INSERT INTO Messages (sender_id, receiver_id, message_content)\n \t\tVALUES (:sender_id:, :receiver_id:, :message_content:)"; $status = $app->modelsManager->executeQuery($phql, array('sender_id' => $sender, 'receiver_id' => $receiver, 'message_content' => $message->message)); $response = new Phalcon\Http\Response(); if ($status->success() == true) { $phql = "SELECT * \n \t\t\tFROM Messages \n \t\t\tWHERE message_id = :message_id:"; $message = $app->modelsManager->executeQuery($phql, array('message_id' => $status->getModel()->message_id))->getFirst(); $response->setStatusCode(200, "Created"); $response->setJsonContent(array('status' => "OK", 'data' => $message)); } else { $response->setStatusCode(400, "Conflict"); $errors = array(); foreach ($status->getMessages() as $errorMessage) { $errors[] = $errorMessage->getMessage(); } $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; }); // Handles the cases where a non-existing resource is called. $app->notFound(function () use($app) { $app->response->setStatusCode(404, "Not Found")->sendHeaders(); echo "You've just typed me, and this is crazy, but this page was not found, so it doesn't exist maybe!"; });
/** * 登録テスト */ public function registAction() { $response = new \Phalcon\Http\Response(); $response->setJsonContent(array("status" => "OK")); return $response; }
$title = $idea->title; $body = $idea->body; $phql = "INSERT INTO Ideas(title, body, added) VALUES(:title:, :body:, :added:)"; $status = $app->modelsManager->executeQuery($phql, ['title' => $title, 'body' => $body, 'added' => new \Phalcon\Db\RawValue('now()')]); $response = new \Phalcon\Http\Response(); if ($status->success()) { $response->setJsonContent(['status' => 'OK']); } }); $app->put('/ideas/{id:[0-9]+}', function ($id) use($app) { $idea = $app->request->getJsonRawBody()->idea; $phql = "UPDATE Ideas SET title = :title:, body = :body: WHERE id = :id:"; $status = $app->modelsManager->executeQuery($phql, ['title' => $idea->title, 'body' => $idea->body, 'id' => $id]); $response = new \Phalcon\Http\Response(); if ($status->success()) { $response->setJsonContent(['status' => 'OK']); } }); $app->delete('/ideas/{id:[0-9]+}', function ($id) use($app) { $status = $app->modelsManager->executeQuery('DELETE FROM Ideas WHERE id = :id:', ['id' => $id]); $response = new \Phalcon\Http\Response(); if ($status->success()) { $response->setJsonContent(['status' => 'OK']); } return $response; }); try { $app->handle(); } catch (\Phalcon\Mvc\Micro\Exception $exception) { echo $exception->getMessage(); }
<?php //Retrieves robots based on primary key $app->get('/api/robots/{id:[0-9]+}', function ($id) use($app) { $phql = "SELECT * FROM Robots WHERE id = :id:"; $robot = $app->modelsManager->executeQuery($phql, array('id' => $id))->getFirst(); //Create a response $response = new Phalcon\Http\Response(); if ($robot == false) { $response->setJsonContent(array('status' => 'NOT-FOUND')); } else { $response->setJsonContent(array('status' => 'FOUND', 'data' => array('id' => $robot->id, 'name' => $robot->name))); } return $response; });