Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

php - How to set Content-Type in Slim to XML?

I want to output application/xml as the Content-Type for my Slim Output. Below you see my code which generates the output. All code that is commented out is unfortunately not working to output the corresponding Content-Type. Do you have any suggestions?

<?php
use PsrHttpMessageResponseInterface as Response;
use PsrHttpMessageServerRequestInterface as Request;
use SlimFactoryAppFactory;
use SlimHelperSet;

require 'vendor/autoload.php';

use Psr7MiddlewaresMiddlewareTrailingSlash;


$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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This should work:

$response = $response->withHeader('Content-Type', 'application/xml');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...