如何在PHP中检查它是xml文件还是json文件

Chr*_*sen 3 php xml json

如果url包含xml的json文件,我如何检查php.所以在$ url中保存url而不是它应该检查它是一个xml文件还是一个json文件.

$url = 'http://hello.asdasd/asdasd/asdasd';

if (($url == JSON) = TRUE){ // how to  check it is a json file, do the follwing action

    $xml = file_get_contents($url);
    $json = json_decode($xml, true);
} 
else if ($url == XML) = TRUE){ // how to check it is a XML file, do the follwing action
    $xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);
}
Run Code Online (Sandbox Code Playgroud)

任何建议都会非常明显.

Jas*_*ary 7

简单过程:

您已经拥有大部分此代码,只需调整if语句以检查尝试解码/加载数据的结果.

此外,由于它是一个Web资源,您可以交叉检查mime类型.这需要使用其他东西file_get_contents().