如何解决如何将 Slim 中的 Content-Type 设置为 XML?
我想输出 Content-Type
作为我的 Slim 输出的 <?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use \Slim\Helper\Set;
require 'vendor/autoload.php';
use Psr7Middlewares\Middleware\TrailingSlash;
$app = AppFactory::create();
$app->get('/{show}/feed',function(Request $request,Response $response,$args) use($app) {
$html = '<?xml version="1.0" encoding="utf-8" ?><sampletag></sampletag>';
// $app->response->headers->set('Content-Type','application/xml');
// $response = $response->withHeader('Content-type','application/xml');
$response->getBody()->write($html);
// return $response->withStatus(201)->withHeader('Content-Type','application/xml')->getBody()->write($html);
return $response;
});
$app->run();
。下面你会看到我生成输出的代码。不幸的是,所有被注释掉的代码都无法输出相应的 Content-Type。您有什么建议吗?
import React from "react";
import { ImageBackground,StyleSheet,Text,View } from "react-native";
const image = { uri: "https://reactjs.org/logo-og.png" };
const App = () => (
<View style={styles.container}>
<ImageBackground source={image} style={styles.image}>
<Text style={styles.text}>Inside</Text>
</ImageBackground>
</View>
);
const styles = StyleSheet.create({
container: {
flex: 1,flexDirection: "column",justifyContent: "center"
},image: {
flex: 1,resizeMode: "cover",justifyContent: "center",margin: "10%"
},text: {
color: "white",fontSize: 42,fontWeight: "bold",textAlign: "center",backgroundColor: "#000000a0"
}
});
export default App;
解决方法
这应该有效:
$response = $response->withHeader('Content-Type','application/xml');
,
您已注释掉负责向响应添加所需标头的行。以下工作正常:
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use \Slim\Helper\Set;
require 'vendor/autoload.php';
use Psr7Middlewares\Middleware\TrailingSlash;
$app = AppFactory::create();
$app->get('/{show}/feed',function(Request $request,Response $response,$args) use($app) {
$html = '<?xml version="1.0" encoding="utf-8" ?><sampletag></sampletag>';
// Do not comment out the following line
$response = $response->withHeader('Content-type','application/xml');
$response->getBody()->write($html);
return $response;
});
$app->run();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。