1

Retrieved the contents of a css file: (http://gizmodo.com/assets/stylesheets/app-ecbc6044c59319aab4c2a1e31380ef56.css)

Detected the encoding with mb_detect_encoding... says UTF-8.

Viewed the page in a browser, looks fine (readable), and declares @charset "UTF-8";

Tried to output the string, got garbage. Tried to save it to a file, got garbage.

Tried to convert the encoding to ASCII, ISO-8859-1, and HTML-ENTITIES. No luck.

Any ideas here how to determine why this string is garbage, and how to fix it?

Matisse VerDuyn
  • 1,148
  • 15
  • 40
  • im with you up to this line *"Tried to output the string in terminal, got garbage"* then i'm not sure what your doing –  Jul 17 '13 at 20:50
  • how did you retrieve the contents of the fiie? the code your using would probably make this clearer –  Jul 17 '13 at 20:52
  • tried both `file_get_contents` and `curl` ... tried on both my local comp and remote server, same result, so i know it's not my config – Matisse VerDuyn Jul 17 '13 at 20:52
  • Hint: UTF-8 equals Latin1 equals ASCII for files containing only ASCII characters. It will never mess up in its entirety because of a charset mismatch. So you can rule out encodings as a problem source. – deceze Jul 17 '13 at 21:02

2 Answers2

5

The Content-Encoding of the page you're trying to fetch is gzip. You'll need to uncompress it before using it.

Notice the Content-Encoding

I just tried the following and it worked fine:

echo gzdecode(file_get_contents($your_url));
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
  • Darn, how'd I miss that! Anyway, you answered first, but @GarrettCosco looks like he's getting started here building rep. Wish I could accept both answers! – Matisse VerDuyn Jul 17 '13 at 21:02
  • 2
    His answer is more "correct" whereas mine is more of a hack/demo. I'd pick his over mine too. – Mr. Llama Jul 17 '13 at 21:03
5
$url = 'http://gizmodo.com/assets/stylesheets/app-ecbc6044c59319aab4c2a1e31380ef56.css';

$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
$data = curl_exec($ch);
curl_close($ch);
echo $data;

Important line is

curl_setopt($ch,CURLOPT_ENCODING , "gzip");
Cosco Tech
  • 565
  • 1
  • 4
  • 22