本文整理汇总了PHP中Response::setBody方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::setBody方法的具体用法?PHP Response::setBody怎么用?PHP Response::setBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Response
的用法示例。
在下文中一共展示了Response::setBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: checkConnection
/**
* check database connection based on provided setting
*/
public function checkConnection()
{
$success = false;
$message = '';
$config = $this->getPostConfiguration();
try {
$this->makeConnection($config);
/**
* Just trying to show tables with current connection
*/
switch ($config['driver']) {
case 'mysql':
$tables = Capsule::select('show tables');
break;
case 'sqlite':
$tables = Capsule::select("SELECT * FROM sqlite_master WHERE type='table'");
break;
case 'sqlsrv':
$tables = Capsule::select("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'");
break;
case 'pgsql':
$tables = Capsule::select("SELECT * FROM pg_catalog.pg_tables");
break;
}
$success = true;
$message = 'Successfully connected!';
} catch (Exception $e) {
$success = false;
$message = $e->getMessage();
}
Response::headers()->set('Content-Type', 'application/json');
Response::setBody(json_encode(array('success' => $success, 'message' => $message, 'config' => $config)));
}
示例2: dispatch
public static function dispatch()
{
$url = new Url();
$uri = $url->getPath();
foreach (array_reverse(self::$routes, true) as $route => $class) {
if (preg_match("~^{$route}\$~", $uri, $params)) {
Router::$current = $class;
$return = call_user_func_array(array('Controller', 'dispatch'), array_merge(array($class), array_slice($params, 1)));
if (!(false === $return)) {
$vars = get_class_vars($class);
$type = 'text/html';
if (isset($vars['type'])) {
$type = $vars['type'];
}
# PHP >= 5.3
# if ( isset($class::$type) )
# $type = $class::$type;
//Response::setHeader('Content-Type', 'application/xhtml+xml');
Response::setHeader('Content-Type', "{$type};charset=UTF-8");
Response::setBody($class, $return);
return;
}
Router::$current = null;
}
}
if (Response::getHttpResponseCode() == 200) {
$class = 'Error404';
$return = Controller::dispatch($class);
Response::setHeader('Content-Type', 'text/html;charset=UTF-8');
Response::setBody($class, $return);
//Response::setHeader('HTTP/1.0 404 Not Found');
//Response::setHttpResponseCode(404);
//Response::setBody('404', 'Error 404');
}
}
示例3: respond
/**
* Sets the response body and status code header
*
* @param mixed $data
* @param integer $status
* @return void
*/
protected function respond($data, $status = null)
{
if ($this->performedRespond) {
throw new DoubleRespond();
}
$this->response->setStatus(!empty($status) ? $status : $this->status);
$this->response->setBody($data);
$this->performedRespond = true;
}
示例4: response
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;
}
示例5: ConvertCurlResponse
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;
}
示例6: error
public static function error($errno)
{
if (isset(self::$errors[$errno])) {
Response::setHeader('Status', self::$errors[$errno]['Status']);
if (isset(self::$errors[$errno]['Class'])) {
self::instance(self::$errors[$errno]['Class']);
} else {
Response::setHeader('Content-Type', 'text/plain');
Response::setBody(self::$errors[$errno]['Status']);
}
}
}
示例7: testToString
function testToString()
{
$response = new Response(200, ['Content-Type' => 'text/xml']);
$response->setBody('foo');
$expected = <<<HI
HTTP/1.1 200 OK
Content-Type: text/xml
foo
HI;
$this->assertEquals($expected, (string) $response);
}
示例8: Auth
function Auth()
{
if (!Sentry::check()) {
if (Request::isAjax()) {
Response::headers()->set('Content-Type', 'application/json');
Response::setBody(json_encode(array('success' => false, 'message' => 'Session expired or unauthorized access.', 'code' => 401)));
App::stop();
} else {
$redirect = Request::getResourceUri();
Response::redirect(App::urlFor('login') . '?redirect=' . base64_encode($redirect));
}
}
}
示例9: outputError
public static function outputError($exception)
{
if (PHP_SAPI == 'cli') {
echo $exception->getMessage() . "\nFile: " . $exception->getFile() . ' on line ' . $exception->getLine() . "\n\n" . $exception->getTraceAsString() . "\n";
// exit with an error code
exit(1);
}
// Write the error to log file
@error_log('Error 404 Page Not Found: ' . $_SERVER['REQUEST_URI']);
$response = new Response();
$response->setStatusCode(404);
$response->setBody(Controller::outputError('errors/404', ['message' => $exception->getMessage()]));
return $response;
}
示例10: testSend
/**
* @runInSeparateProcess
*
* Unfortunately we have no way of testing if the HTTP response code got
* changed.
*/
function testSend()
{
if (!function_exists('xdebug_get_headers')) {
$this->markTestSkipped('XDebug needs to be installed for this test to run');
}
$response = new Response(204, ['Content-Type', 'text/xml']);
$response->setBody('foo');
ob_start();
Sapi::sendResponse($response);
$headers = xdebug_get_headers();
$result = ob_get_clean();
header_remove();
$this->assertEquals(["0: Content-Type", "1: text/xml"], $headers);
$this->assertEquals('foo', $result);
}
示例11: doExecute
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;
}
示例12: checkConnection
/**
* check database connection based on provided setting
*/
public function checkConnection()
{
$success = false;
$message = '';
$config = $this->getPostConfiguration();
try {
$this->makeConnection($config);
$tables = Capsule::select('show tables');
$success = true;
$message = 'Successfully connected!';
} catch (Exception $e) {
$success = false;
$message = $e->getMessage();
}
Response::headers()->set('Content-Type', 'application/json');
Response::setBody(json_encode(array('success' => $success, 'message' => $message, 'config' => $config)));
}
示例13: testSend
/**
* @runInSeparateProcess
*
* Unfortunately we have no way of testing if the HTTP response code got
* changed.
*/
function testSend()
{
if (!function_exists('xdebug_get_headers')) {
$this->markTestSkipped('XDebug needs to be installed for this test to run');
}
$response = new Response(204, ['Content-Type' => 'text/xml;charset=UTF-8']);
// Second Content-Type header. Normally this doesn't make sense.
$response->addHeader('Content-Type', 'application/xml');
$response->setBody('foo');
ob_start();
Sapi::sendResponse($response);
$headers = xdebug_get_headers();
$result = ob_get_clean();
header_remove();
$this->assertEquals(["Content-Type: text/xml;charset=UTF-8", "Content-Type: application/xml"], $headers);
$this->assertEquals('foo', $result);
}
示例14: outputError
public static function outputError($message = null, $file = false, $line = false, $trace = false)
{
// Message for log
$errorMessage = 'Error ' . $message . ' in ' . $file . ' line: ' . $line;
// Write the error to log file
@error_log($errorMessage);
// Just output the error if the error source for view file or if in cli mode.
if (PHP_SAPI == 'cli') {
exit($errorMessage);
}
$code = [];
if (!$file) {
goto constructViewData;
}
$fileString = file_get_contents($file);
$arrLine = explode("\n", $fileString);
$totalLine = count($arrLine);
$getLine = array_combine(range(1, $totalLine), array_values($arrLine));
$startIterate = $line - 5;
$endIterate = $line + 5;
if ($startIterate < 1) {
$startIterate = 1;
}
if ($endIterate > $totalLine) {
$endIterate = $totalLine;
}
for ($i = $startIterate; $i <= $endIterate; $i++) {
$html = '<span style="margin-right:10px;background:#CFCFCF;">' . $i . '</span>';
if ($line == $i) {
$html .= '<span style="color:#DD0000">' . htmlentities($getLine[$i]) . "</span>\n";
} else {
$html .= htmlentities($getLine[$i]) . "\n";
}
$code[] = $html;
}
constructViewData:
$data = ['message' => $message, 'file' => $file, 'line' => $line, 'code' => $code, 'trace' => $trace];
$response = new Response();
$response->setStatusCode(500);
$response->setBody(Controller::outputError('errors/500', $data));
return $response;
}
示例15: runApp
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);
}
}