收到的 HTTP 标头对于 XSL 文件类型不正确/意外
我正在使用 PHP+curl 检索 XSL 文件,但首先我想做一些最低限度的验证,以确保该文件确实是 XSL 文件。
我确保文件存在,状态代码为 200 OK,并且文件扩展名正确,但随后我想检查 Content-Type 标头。 我使用 curl_getinfo()
函数来执行此操作。 一切都很好。
但 XSL 文件始终返回“application/xml
”或“text/xml
”作为 Content-Type,这适用于 XML 文件。 我相信 XSL 标头应该是“application/xslt+xml
”。 那么这里发生了什么以及如何确保它是一个 XSL 文档?
I'm using PHP+curl to retrieve an XSL file, but first I want to do some minimum validation that the file is really a XSL file.
I make sure the file exists, has status code 200 OK, and that it has the correct file extension, but then I want to check the header for the Content-Type. I do this using curl_getinfo()
function. All fine.
But XSL files always return "application/xml
" or "text/xml
" as the Content-Type, which is for XML files. I believe an XSL header should be "application/xslt+xml
". So what's going on here and how can I make sure it's an XSL document?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以检查文件中的 XSLT 命名空间。
如果 PHP 版本 >= 5.1.2,请使用
SimpleXMLElement::getNamespaces()
You can check the XSLT namespace in the file.
If PHP version >= 5.1.2, use
SimpleXMLElement::getNamespaces()
您是否使用它来访问第三方网站或您自己配置的网站? 如果这是您自己的站点,则需要设置 Apache 以根据文件扩展名提供适当的 MIME 类型。
如果是第三方网站,我不确定您是否可以依赖哑剧类型,因为它们可以提供他们想要的任何服务。 最好的办法可能是只获取文件的前几行并检查根元素是否为 xsl。
Are you using this to access third-party sites or a site that you configure yourself? If it's your own site, you'd need to set up Apache to serve the appropriate mime types depending on the file extension.
If it's third party sites, I'm not sure you can rely on the mime types, since they could serve whatever they want. Best bet is probably to just get the first few lines of the file and check that the root element is xsl.
对于 PHP 5,不同的方法:
Different approach, for PHP 5: