PHP Curl UTF-8字符集 [英] PHP Curl UTF-8 Charset
问题描述
我有一个php脚本,调用另一个网页,并写入所有的HTML页面,一切正常,但有一个字符集的问题。我的php文件编码是utf-8和所有其他php文件工作正常(这意味着没有问题的服务器)。在那个代码中缺少的东西是什么,所有的西班牙字母看起来很奇怪。 PS。当我把这些古怪的字符原始版本写入php时,它们都看起来准确。
I have an php script which calls another web page and writes all the html of the page and everything goes ok however there is a charset problem. My php file encoding is utf-8 and all other php files work ok (that means there is no problem with server). What is the missing thing in that code and all spanish letters look weird. PS. When I wrote these weird characters original versions into php, they all look accurate.
header("Content-Type: text/html; charset=utf-8");
function file_get_contents_curl($url)
{
$ch=curl_init();
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
$data=curl_exec($ch);
curl_close($ch);
return $data;
}
$html=file_get_contents_curl($_GET["u"]);
$doc=new DOMDocument();
@$doc->loadHTML($html);
推荐答案
简单:
当你使用curl将字符串编码为 utf-8
,您只需要解码它们。
Simple:
When you use curl it encodes the string to utf-8
you just need to decode them..
Description
string utf8_decode ( string $data )
数据,假定为 UTF-8
编码为 ISO-8859-1
。
This function decodes data , assumed to be UTF-8
encoded, to ISO-8859-1
.
这篇关于PHP Curl UTF-8字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!