Matisse VerDuyn
Matisse VerDuyn

Reputation: 1138

PHP File Get Contents & String Encoding

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?

Upvotes: 1

Views: 2196

Answers (2)

Cosco Tech
Cosco Tech

Reputation: 575

$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");

Upvotes: 5

Mr. Llama
Mr. Llama

Reputation: 20889

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));

Upvotes: 5

Related Questions