php-curl Windows-1253编码为utf8的问题

时间:2019-05-14 12:25:07

标签: curl iframe utf-8 php-curl mb-convert-encoding

我正在使用以下代码来将数据显示到php页面上:

$url = 'http://example.com';

//Initiate cURL and pass it the URL we want to retrieve.
$ch = curl_init($url);

//Tell cURL to return the output as a string.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

//Execute the cURL request and return the output to a string.
$response = curl_exec($ch);

//Print out the response to the browser.
echo mb_detect_encoding($response);
echo utf8_encode($response);

最后两行包含我最后尝试过的调试方法。 即使原始源URL的字符集中包含Windows-1253编码,Mb_detect_encodign也会在我的内容上返回UTF-8。

内容显示不正确-它会返回类似õìðëçñþóôå之类的字符,而不是希腊字符所期望的原始内容。

我知道PHP不支持Windows-1253,但是,似乎phpcurl正在将其转换为UTF8-但就我而言,它没有正确完成。

我尝试添加一个没有运气的php标头。还尝试添加mb_convert_encoding,也没有运气。

有人建议吗?

1 个答案:

答案 0 :(得分:0)

通过更改为file_get_contents解决了这个问题:

function file_get_contents_utf8($fn) { 
     $content = file_get_contents($fn); 
      return mb_convert_encoding($content, 'UTF-8', 
          mb_detect_encoding($content, 'UTF-8, ISO-8859-7', true)); 
} 

print file_get_contents_utf8('http://example.com/');