PHP文件获取内容&字符串编码 [英] PHP File Get Contents & String Encoding

查看:229
本文介绍了PHP文件获取内容&字符串编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

检索css文件的内容:( http://gizmodo.com/assets /stylesheets/app-ecbc6044c59319aab4c2a1e31380ef56.css



使用 mb_detect_encoding 检测到编码...

在浏览器中查看页面,看起来很好(可读),并声明 @charsetUTF-8 code>



尝试输出字符串,得到垃圾。
尝试将其保存到文件,有垃圾。



尝试将编码转换为ASCII,ISO-8859-1和HTML-ENTITIES。没有运气。



这里的任何想法如何确定为什么这个字符串是垃圾,以及如何解决它?

解决方案

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

这篇关于PHP文件获取内容&字符串编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆