PHP可以';无法从中加载XML文件http://comment.bilibili.tv
我使用下面的简单代码从Internet加载XML文件并读取它:PHP可以';无法从中加载XML文件http://comment.bilibili.tv,php,xml,dom,simplexml,file-get-contents,Php,Xml,Dom,Simplexml,File Get Contents,我使用下面的简单代码从Internet加载XML文件并读取它: $doc = simplexml_load_file($url); 或此代码: $doc = new DOMDocument(); $doc->load($url); 或tis代码: $doc = file_get_contents($url); 除了一个网站外,它们运行良好: 如果$url类似于“”, file_get_contents()将获得凌乱的代码,simplexml_load_file()将获得空对象 但如果
$doc = simplexml_load_file($url);
或此代码:
$doc = new DOMDocument();
$doc->load($url);
或tis代码:
$doc = file_get_contents($url);
除了一个网站外,它们运行良好:
如果$url类似于“”,
file_get_contents()将获得凌乱的代码,simplexml_load_file()将获得空对象
但如果我将其下载到本地硬盘,并将$url更改为“681965.xml”,
代码是有效的
那有什么问题?如何修复它?来自comment.bilibili.tv的响应使用该算法进行压缩,当您在web浏览器中请求时,您可以从
内容编码
标题中看到这一点
我不知道为什么PHP在您请求并为您处理它时没有注意到这一点,但您可以在调用文件获取内容后通过gzflate
传递内容来绕过它。然后您可以根据需要将内容加载到DOMDocument
或simplexml\u load\u string
中
$content = gzinflate(file_get_contents("http://comment.bilibili.tv/681965.xml"));
您还可以使用来构建一个URL,该URL可以直接传递到simplexml\u load\u file
或DOMDocument::load
$url = "php://filter/zlib.inflate/resource=http://comment.bilibili.tv/681965.xml";
$doc = simplexml_load_file($url);
$doc = new DOMDocument();
$doc->load($url);
$doc = file_get_contents($url);
您需要更具体地说明所看到的错误。“把代码弄得乱七八糟”是什么意思?您看到错误消息了吗?你有没有试过做一些类似的事情,比如$doc=file\u get\u contents($url);文件内容('test.txt',$doc)代码>并在文本编辑器中打开它?如果是,你能描述一下你的发现吗,或者给我们举个小例子?