/** * 发送响应消息 * * @param string $message 响应信息 * @param int $status 响应状态码 */ public static function send($message = '', $status = 200) { if (self::$response === null) { self::init(); } self::$response->setStatusCode(intval($status)); self::$response->setContent(strval($message)); self::$response->send(); }
/** * 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(); }
/** * 发送数据 * @param mixed $result */ private static function sendResult($result) { $response = new Response(); $response->setHeader('Content-Type', 'application/json; charset=UTF-8'); $response->setHeader('Access-Control-Allow-Origin', '*'); $response->setJsonContent($result); $response->send(); exit; }
/** * 向Client发送响应的资源 */ public function send() { if (is_array($this->resource) || is_object($this->resource)) { $this->response->setHeader('Content-Type', 'text/json'); $this->response->setContent(json_encode($this->resource, true)); } else { $this->response->setContent($this->resource); } $this->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(); }
public function debugEnvAction() { //var_dump($_SERVER['REDIRECT_URL']);die(); $actual_route = $_SERVER['REDIRECT_URL']; $response = new Response(); $response->setStatusCode(404, "Not Found"); $response->setContent("Sorry, the route: <b>" . $actual_route . " </b> doesn't exist"); $response->send(); return $response; }
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(); }
/** * Main run block that executes the micro application * */ public function run() { // Handle any routes not found $this->notFound(function () { $response = new Response(); $response->setStatusCode(404, 'Not Found')->sendHeaders(); $response->setContent('Page doesn\'t exist.'); $response->send(); }); $this->handle(); }
public function send() { return parent::send(); }
/** * 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 download($url, $destination = "", $file_name = "") { // check URL and get token if necessary preg_match("/(.*)periscope\\.tv\\/w\\/(.*)/", trim($url), $output_array); if (isset($output_array[2])) { $periscope_token = $output_array[2]; } else { preg_match("/(.*)watchonperiscope\\.com\\/broadcast\\/(.*)/", trim($url), $output_array); if (isset($output_array[2])) { try { $watchonperiscope_response = $this->_guzzle->get("https://watchonperiscope.com/api/accessChannel?broadcast_id=" . $output_array[2])->getBody(); } catch (\GuzzleHttp\Exception\ServerException $e) { throw new \Exception("URL error: Invalid watchonperiscope.com URL", 2); } $watchonperiscope_json = json_decode($watchonperiscope_response, true); if (!isset($watchonperiscope_json["error"])) { preg_match("/(.*)periscope\\.tv\\/w\\/(.*)/", $watchonperiscope_json["share_url"], $output_array); $periscope_token = $output_array[2]; } else { throw new \Exception("URL error: Invalid watchonperiscope.com URL", 2); } } else { throw new \Exception("URL error: Unsupported URL", 1); } } // construct filename and destination if ($file_name == "") { try { $periscope_details_response = $this->_guzzle->get("https://api.periscope.tv/api/v2/getBroadcastPublic?broadcast_id=" . $periscope_token)->getBody(); } catch (\GuzzleHttp\Exception\ClientException $e) { throw new \Exception("Periscope error: Invalid token", 3); } $periscope_details_json = json_decode($periscope_details_response, true); $periscope_user = $periscope_details_json["user"]["username"]; $periscope_start_time = $periscope_details_json["broadcast"]["start"]; $date = substr($periscope_start_time, 0, 10); $hours = substr($periscope_start_time, 11, 2); $mins = substr($periscope_start_time, 14, 2); $file_name = $periscope_user . "_" . $date . "_" . $hours . "_" . $mins . ".ts"; } else { $file_name = rtrim($file_name, ".ts") . ".ts"; } if ($destination == "") { $destination = __DIR__ . "/"; } else { $destination = rtrim($destination, "/") . "/"; } // set up cookies try { $periscope_cookies_response = $this->_guzzle->get("https://api.periscope.tv/api/v2/getAccessPublic?broadcast_id=" . $periscope_token)->getBody(); } catch (\GuzzleHttp\Exception\ClientException $e) { throw new \Exception("Periscope error: Invalid token", 3); } $periscope_cookies_json = json_decode($periscope_cookies_response, true); $replay_url = $periscope_cookies_json["replay_url"]; $base_url = str_replace("/playlist.m3u8", "", $replay_url); $cookies = array(); foreach ($periscope_cookies_json["cookies"] as $cookie) { $cookies[$cookie["Name"]] = $cookie["Value"]; } $cookie_jar = new \GuzzleHttp\Cookie\CookieJar(); $periscope_cookies = $cookie_jar::fromArray($cookies, "replay.periscope.tv"); // download playlist and all chunks $periscope_playlist_response = $this->_guzzle->get($replay_url, ["cookies" => $periscope_cookies])->getBody()->getContents(); preg_match_all("/chunk_(.*)\\.ts/", $periscope_playlist_response, $chunk_array); $tmp_folder = $destination . "/" . bin2hex(openssl_random_pseudo_bytes(16)) . "/"; shell_exec("mkdir " . $tmp_folder); $path = $destination . $file_name; if (!file_exists($path)) { shell_exec("cat " . $tmp_folder . $chunk_array[0][0] . " >> " . $path); } $response = new Response(); $filetype = filetype($path); $filesize = filesize($path); while (ob_get_level()) { ob_end_clean(); } $response->setHeader("Content-Description", 'File Transfer'); $response->setHeader("Cache-Control", 'must-revalidate, post-check=0, pre-check=0'); $response->setHeader("Content-Disposition", 'attachment; filename=' . $file_name); $response->setHeader("Content-Type", $filetype); $response->setHeader("Content-Length", $filesize); $response->setHeader("Content-Transfer-Encoding", 'binary'); $response->setHeader("Expires", '0'); $response->setHeader("Pragma", 'public'); $response->setFileToSend($path, null, false); $response->send(); foreach ($chunk_array[0] as $chunk) { $chunk_response = $this->_guzzle->get($base_url . "/" . $chunk, ["cookies" => $periscope_cookies])->getBody()->getContents(); //, 'stream' => true // $chunk_size = $chunk_response->getHeader('content-length'); // $body = $chunk_response->getBody(); // // while ( ! $body->eof()) { // echo $body->read($chunk_size[0]); // } file_put_contents($tmp_folder . $chunk, $chunk_response); $chunk_size = filesize($tmp_folder . $chunk); if (file_exists($path)) { shell_exec("cat " . $path . ' ' . $tmp_folder . $chunk . " >> " . $path); } $fh = fopen($tmp_folder . $chunk, "rb"); echo fread($fh, $chunk_size); while (ob_get_level()) { ob_end_clean(); } flush(); fclose($fh); } // clean up shell_exec("rm -rf " . $tmp_folder); exit; return $destination . $file_name; }
/** * @param \Exception $exception * * @return \Phalcon\Http\ResponseInterface|void */ public function sendException(\Exception $exception) { $this->setStatusCode($exception->getCode(), $exception->getMessage()); $this->setJsonContent($this); return parent::send(); }
/** * Save page layout with content. * * @param int $id Page identity. * * @return ResponseInterface * * @Route("/save-layout/{id:[0-9]+}", methods={"POST"}, name="admin-pages-save-layout") */ public function saveLayoutAction($id) { $response = new Response(); $response->setStatusCode(200, "OK"); $response->setContent(json_encode(["error" => 0])); $layout = $this->request->get("layout"); $items = $this->request->get("items"); // Save page with widgets and layout. $page = Page::findFirstById($id); $page->layout = $layout; $page->setWidgets($items); $page->save(); // Clear widgets cache. /** @var \Phalcon\Cache\BackendInterface $cache */ $cache = $this->getDI()->get('cacheOutput'); $prefix = $this->config->application->cache->prefix; $widgetKeys = $cache->queryKeys($prefix . WidgetController::CACHE_PREFIX); foreach ($widgetKeys as $key) { $cache->delete(str_replace($prefix, '', $key)); } $this->flashSession->success('Page saved!'); return $response->send(); }
*/ require ROOT_DIR . 'core/config/services.php'; /** * Handle the request */ $application = new Application(); /** * Assign the DI */ $application->setDI($di); /** * Include modules */ $application->registerModules(require ROOT_DIR . 'core/config/modules.php'); /** * Sets the event manager */ $application->setEventsManager($eventsManager); echo $application->handle()->getContent(); } catch (Exception $e) { echo $e->getMessage(); echo $e->getTraceAsString(); /** * Show an static error page */ if (!$di->get('config')->application->debug) { $response = new Response(); $response->redirect('errors/503'); $response->send(); } }
/** * 用户注销页 */ public function getSignOutAction() { $this->session->has('auth') and $this->session->remove('auth'); $response = new Response(); $response->redirect(isset($_GET['callback']) ? $_GET['callback'] : $this->url->get('signin'), true); $response->send(); }
/** * Prints out HTTP response to the client * And if isset request 'suppress_response_codes', then set status code 200 * @return \Phalcon\Http\ResponseInterface */ public function send() { $request = $this->getDI()->get('request'); if ($request->get('suppress_response_codes', null, null)) { $this->setStatusCode(self::OK)->sendHeaders(); } return parent::send(); }
public function downloadAction() { $this->view->disable(); $filename = $this->request->getPost("filename"); $json_string = $this->request->getPost("json_content"); $response = new \Phalcon\Http\Response(); $response->setHeader("Content-Type", "application/json"); $response->setHeader("Content-Disposition", 'attachment; filename="' . $filename . '"'); $response->setHeader("Content-Length", strlen($json_string)); $response->setContent($json_string); $response->send(); }
/** * * @param type $fileName * @param type $fileTemp * @return type */ private function download($fileName, $fileTemp) { $this->view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_NO_RENDER); $response = new Response(); $response->setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); $response->setHeader('Content-Disposition', 'attachment;filename="' . $fileName . '"'); $response->setHeader('Cache-Control', 'max-age=0'); //$response->setHeader('Cache-Control', 'max-age=1'); $response->setContent(file_get_contents($fileTemp)); unlink($fileTemp); return $response->send(); }
/** * 處理錯誤資訊 for JSON * * @param Exception $e Exception物件 * @param Response $response Phalcon Response Object * * @return void */ public static function handleJson($e, $response) { if (!$response) { $response = new Response(); } $result = self::handle($e); $response->setStatusCode($result['httpCode'], self::$headerAry[$result['httpCode']]); $response->setJsonContent(['status' => 'fail', 'code' => $result['code'], 'message' => $result['message'], 'detail' => $result['detail']]); $response->send(); }