1

我正在使用此代码将数据显示到 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);

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

内容显示不正确 - 它返回如下字符:õìðëçñþóôå,而不是希腊字符中预期的原始内容。

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

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

有什么建议吗?

4

1 回答 1

1

通过更改为解决此问题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/');
于 2019-05-14T13:11:31.450 回答