我正在尝试获取网站的源代码,以便我可以解析足球比赛,我的代码是:
<?php
$url = "https://www.bbc.co.uk/sport/football/scores-fixtures/2019-03-06";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-gb,en;q=0.5',
'Accept-Encoding: gzip, deflate',
'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Proxy-Connection: Close',
'Cookie: PREF=ID=2bb051bfbf00e95b:U=c0bb6046a0ce0334:',
'Cache-Control: max-age=0',
'Connection: Close'
));
$output = curl_exec($ch);
curl_close($ch);
echo substr($output, 0, 12);
?>
显示的子字符串的输出是:
���
我需要标准文本的输出,是压缩的还是其他的?
请问我该如何解决这个问题?
谢谢。
最佳答案
I need the output in standard text, is that compressed or something?
是的,正是这样:它是 gzip 压缩的。您的选择是
a)使用例如解压缩它gzdecode
b) 告诉服务器您不需要 gzip 编码的响应;最简单的方法是让curl 为您处理这个问题:
- 从 header 数组中删除
'Accept-Encoding: gzip, deflate',
- 在
curl_exec()
之前添加:curl_setopt($ch, CURLOPT_ENCODING, 'identity');
关于php - CURL 返回奇怪的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55018532/