且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP文件获取内容&字符串编码

更新时间:2023-02-23 10:00:25

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

重要行是

  curl_setopt($ ch,CURLOPT_ENCODING,gzip); 


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?

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