使用 file_get_contents() 和 ajax: 字符获取 HTML 无法正确显示


Getting HTML with file_get_contents() and ajax: chars are not displayed properly

我用我的代理从网站检索html数据.php和main.js。以下是相关摘录:

代理.php:

<?php
$url = addslashes($_GET['url']);
$output = file_get_contents($url);
echo $output;

阿贾克斯.js:

    $.ajax({
        url     : 'proxy.php?url=http://www.example.com',
        cache   : false,
        dataType: 'html'
    })
    .done(function (html) {
        // do something
    });

我的问题是德语中的"ä"、"ö"、"ü"等特殊字符无法正确显示。例如,我得到的不是"Nächte",而是输出"Nächte"。有人知道这个问题的解决方案吗?

在 proxy.php 文件中指定标头可能有助于确保使用适当的字符编码。

例如
<?php
    header('Content-Type: text/html; charset=utf-8');
    $url = addslashes($_GET['url']);
    $output = file_get_contents($url);
    echo $output;
?>