问题描述
我在使用 laravel 控制器导出 kml 和 gpx 文件时遇到问题:对于 json 导出,我使用函数
public function mapExportJSON(Request $request) {
$client = new Client();
$http = $client->post(
'https://myurl',[
'body' => json_encode(array('points' => $request->points)),'headers' => [
'Cache-Control' => 'no-cache','Content-type' => 'application/json',]
]
);
$result = $http->getBody()->getContents();
return response($result)
->header('Content-disposition','attachment; filename=route.json');
}
我对 xml 使用格式化程序,但它不起作用:(
public function mapExportKML(Request $request) {
$client = new Client();
$http = $client->post(
'https://myurl',]
]
);
$result = $http->getBody();
$formatter = Formatter::make($result,Formatter::JSON);
$xml = $formatter->toXml();
return response($xml)
->header('Content-disposition','attachment; filename=route.kml')
->header('Content-Type','application/xml');
}
有人帮我吗?
解决方法
看起来您使用了不正确的格式化程序,请尝试以下操作
$formatter = Formatter::make($result,Formatter::XML);
$xml = $formatter->toXml();