php file_get_contents() 将 UTF-8 转换为 ISO-8859-1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5600371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
file_get_contents() converts UTF-8 to ISO-8859-1
提问by vladinko0
I am trying to get search results from yahoo.com.
我正在尝试从yahoo.com获取搜索结果。
Butfile_get_contents() converts UTF-8 charset (charset, that yahoo uses) content to ISO-8859-1.
但是file_get_contents() 将 UTF-8 字符集(雅虎使用的字符集)内容转换为 ISO-8859-1。
Try:
尝试:
$filename = "http://search.yahoo.com/search;_ylt=A0oG7lpgGp9NTSYAiQBXNyoA?p=naj%C5%A1%C5%A5astnej%C5%A1%C3%AD&fr2=sb-top&fr=yfp-t-701&type_param=&rd=pref";
echo file_get_contents($filename);
Scripts as
脚本为
header('Content-Type: text/html; charset=UTF-8');
or
或者
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
or
或者
$er = mb_convert_encoding($filename , 'UTF-8');
or
或者
$s2 = iconv("ISO-8859-1","UTF-8",$filename );
or
或者
echo utf8_encode(file_get_contents($filename));
NOT help, because after getting web content speciall characters as ? ? ? are replaced with question marks ???
没有帮助,因为在获得网络内容特殊字符后?? ? 用问号代替???
I would appreciate any kind of help.
我将不胜感激任何形式的帮助。
回答by Gumbo
This seems to be a content negotiationproblem as file_get_contents
probably sends a request that only accepts ISO 8859-1 as character encoding.
这似乎是一个内容协商问题,因为file_get_contents
可能发送了一个只接受 ISO 8859-1 作为字符编码的请求。
You can create a custom stream contextfor file_get_contents
using stream_context_create
that explicitly states that you accept UTF-8:
您可以创建一个自定义流上下文以file_get_contents
使用stream_context_create
明确声明您接受 UTF-8:
$opts = array('http' => array('header' => 'Accept-Charset: UTF-8, *;q=0'));
$context = stream_context_create($opts);
$filename = "http://search.yahoo.com/search;_ylt=A0oG7lpgGp9NTSYAiQBXNyoA?p=naj%C5%A1%C5%A5astnej%C5%A1%C3%AD&fr2=sb-top&fr=yfp-t-701&type_param=&rd=pref";
echo file_get_contents($filename, false, $context);
回答by Evert
file_get_contents should notchange the charset. The data is pulled in as a binary string.
的file_get_contents应该不会改变的字符集。数据以二进制字符串形式拉入。
When checking out the url you provided, this is the header it provides:
检查您提供的网址时,这是它提供的标题:
Content-Type: text/html; charset=ISO-8859-1
Also, in the body:
此外,在体内:
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
Also, you can't convert UTF-8 losslessly convert to ISO-8859-1 and get the characters back when going back to UTF-8. UTF-8 / unicode supports many many more characters, so the characters are lost in the first step.
此外,您无法将 UTF-8 无损转换为 ISO-8859-1 并在返回 UTF-8 时取回字符。UTF-8 / unicode 支持更多的字符,因此在第一步中字符会丢失。
In the browser this is not the case, so perhaps you just need to provide a correct Accept-Encoding header to instruct yahoo's system you can accept UTF-8.
在浏览器中,情况并非如此,所以也许您只需要提供正确的 Accept-Encoding 标头来指示雅虎系统您可以接受 UTF-8。
回答by Dejan Marjanovic
$s2 = iconv("ISO-8859-1","UTF-8//TRANSLIT//IGNORE",$filename );
Better solution...
更好的解决方案...
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, 1);
return curl_exec($ch);
curl_close($ch);
}
echo curl($filename);
回答by Stavros
For anyone investigating on this:
对于任何对此进行调查的人:
The time I spent on encoding issues taught me that rarely php functions "magically" change the encoding of strings. (One of these rare examples is :
我花在编码问题上的时间告诉我,很少有 php 函数“神奇地”改变字符串的编码。(这些罕见的例子之一是:
exec( $command, $output, $returnVal )
Please note also that the working header set is as follows:
另请注意,工作标头集如下:
header('Content-Type: text/html; charset=utf-8');
and not:
并不是:
header('Content-Type: text/html; charset=UTF-8');
As I had a similar issue as the one you describe, it was enough to set the headers properly.
由于我遇到了与您描述的问题类似的问题,因此正确设置标题就足够了。
Hope this helps!
希望这可以帮助!