Reputation: 546
I'm trying to grab the source code of a website so I can parse out football fixtures, my code is:
<?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);
?>
Output of the substring shown is:
���
I need the output in standard text, is that compressed or something?
How do I fix this please?
Thanks.
Upvotes: 2
Views: 2477
Reputation: 16048
I need the output in standard text, is that compressed or something?
Yes, exactly that: it's gzip-compressed. Your options are
a) decompress it using e.g. gzdecode
b) tell the server you don't want a gzip-encoded response; the easiest way is to let curl handle this for you:
'Accept-Encoding: gzip, deflate',
from your header arraycurl_setopt($ch, CURLOPT_ENCODING, 'identity');
somewhere before you curl_exec()
Upvotes: 3