Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k 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 Reply

0 votes
by (71.8m points)

This should work:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...