PHP File Get Contents & String Encoding

纵饮孤独 提交于 2020-01-24 20:01:09

问题


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?


回答1:


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



回答2:


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

I just tried the following and it worked fine:

echo gzdecode(file_get_contents($your_url));


来源:https://stackoverflow.com/questions/17709888/php-file-get-contents-string-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!