尝试从以下网址阅读XML Feed:http://www.mister-baseball.com/feed/
然而,输出以某种方式编码错误,我无法弄清楚如何解决这个问题。我的代码如下:
$url = "http://www.mister-baseball.com/feed/";
// Check the cache first
$error = false;
if (!file_exists($cacheFile) || filemtime($cacheFile) < (time() - 60 * 5)) {
if ($xmlString = @file_get_contents($url, FILE_TEXT)) {
// WHEN I PRINT THE $xmlString HERE, IT LOOKS BAD
file_put_contents($cacheFile, $xmlString, LOCK_EX);
} else {
// THIS PART IS NOT REACHED
$logDate = date('d-M-Y H:i:s');
error_log("[$logDate] Cannot open XML news feed at $url");
$error = true;
}
}
答案 0 :(得分:3)
网址正在返回一个gzip。写完缓存文件后,用winrar或7zip打开它,你会看到。
答案 1 :(得分:1)
PHP内置了这个功能。
http://php.net/manual/en/wrappers.compression.php
您可以使用:
simplexml_load_file("compress.zlib://http://www.mister-baseball.com/feed/")
OR
file_get_contents('compress.zlib://http://www.mister-baseball.com/feed/');