php - 为什么 file_get_contents 返回奇怪的字符?

标签 php parsing

我正在尝试使用 file_get_contents 解析 http://www.desi-tashan.com/category/pakistan-tvs/aaj-tv/3-idiots/

但它返回非常不寻常的字符和符号。

好像我解析 http://www.desi-tashan.com/ 它工作得很好。有人能告诉为什么会这样吗?

是否涉及任何编码解码?

该页面似乎是用wordpress制作的..

最佳答案

你看到的内容是gzipped
您可能对 gzdecode zlib-decode 感兴趣(请注意 PHP 中的 Zlib 支持默认未启用)

您的代码可能如下所示

$url = 'http://www.desi-tashan.com/category/pakistan-tvs/aaj-tv/3-idiots/';
$content = file_get_contents($url);
$decoded_content = gzdecode($content); // or zlib_decode($content);

stackoverflow 上的另一个解决方案 here,它在请求中添加了 HTTP header Accept-Encoding,告诉服务器不要 gzip。

但是,它不适用于 www.desi-tashan.com ,服务器忽略 Accept-Encoding header ,并始终返回 gzipped 内容

关于php - 为什么 file_get_contents 返回奇怪的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12634139/

相关文章:

json - 解码并从 json 数组中获取第 n 项

python - 使用Python将数据帧索引到Elasticsearch中

php - Symfony:清除 Doctrine 缓存

php - 如何在 PHP 中重定向到同一页面,但没有某些参数?

php - 使用 PHP 生成和分发唯一的用户名

php - 如何使用 PHP 解析 .pls 文件? parse_ini_file 有问题

php - 使用php查找并编辑html的注释元素

php - PHP 上传和 MySQL 插入后的白色空图像

python - 在 python(pylab) 中解析这个 csv 文件并将其转换为字典

java - 本地日期 : parse MM-yyyy