【问题标题】:HTML and PHP cURL response utf-8 encoding problemHTML 和 PHP cURL 响应 utf-8 编码问题
【发布时间】:2021-10-27 15:23:18
【问题描述】:

我正在从两个网站的 cURL 获取 HTML。

站点 1: https://xperia.sony.jp/campaign/360RA/?s_tc=somc_co_ext_docomo_360RA_banner

站点 2: https://www.fidelity.jp/fwe-top/?utm_source=outbrain&utm_medium=display&utm_campaign=similar-gdw&utm_content=FS001&dicbo=v1-b6eb7c5f86a6978bba74e3703a046886-00d8ad90c4cb65b2bdcc239bcccf5ec378-mnrtcytfgu4toljwgjrwgljumu4wmljzg5tgkljxgzsdgzbqmyzwenbsgy

我的 cURL 看起来像:

$ua= "Mozilla/5.0 (X11; Linux i686; rv:36.0) Gecko/20100101 Firefox/36.0 SeaMonkey/2.33.1";     
$options = array(
                CURLOPT_RETURNTRANSFER => true, // return web page
                CURLOPT_FAILONERROR => true, 
                CURLOPT_FOLLOWLOCATION => true, // follow redirects
                CURLOPT_ENCODING => "", // handle all encodings 
                CURLOPT_USERAGENT => $ua, // who am i
                
                       
                CURLOPT_AUTOREFERER => true, // set referer on redirect
                CURLOPT_CONNECTTIMEOUT => 10, // timeout on connect
                CURLOPT_TIMEOUT => 10, // timeout on response
                CURLOPT_MAXREDIRS => 5,
                CURLOPT_FORBID_REUSE, true);
        
        $ch = curl_init($url);
            curl_setopt_array($ch, $options);
            $content = curl_exec($ch);

         //Use xPath or str_get_html($content) to parse

第一个 URL 以完美编码打开并按预期显示字符

Exaple: $title_string = $html->find("title",0)->plaintext shows the <title> tag text and characters well encoded

第二个 URL 显示 SQUARE BOXES ¤ããªãããi��Ɨ� 。但是当您执行utf8_decode( $title_string) 时,此第二个 URL 将按预期显示编码良好的字符。

问题是,当您使用utf8_decode( $title_string) 时,FIRST URL 现在显示SQUARE BOXES

有没有一种通用的方法来解决这个问题?

我试过了

$charset=  mb_detect_encoding($str);
    if( $charset=="UTF-8" ) {
        return utf8_decode($str);
    }
    else {
        return $str;
    }

似乎两个字符串都被 cURL 编码为 UTF-8。一个有效,另一个显示方形框。

我也试过

php curl response encoding

Strange behaviour when encoding cURL response as UTF-8

Replace unicode character

https://www.php.net/manual/en/function.mb-convert-encoding.php

Which charset should i use for multilingual website?

French and Chinese characters are not appearing correctly

还有更多

我花费了关键的时间来解决这个问题。欢迎任何想法

【问题讨论】:

  • xperia 网站包含明确的&lt;head&gt; &lt;meta charset="utf-8"&gt; …fidelity 没有?
  • 一种将两者都编码为 UTF-8 的方法?你!
  • 我仍然可以看到链接ctrlv.link/CV8A 添加CURLOPT_ENCODING =&gt; 'UTF-8'
  • CURLOPT_ENCODING 是关于内容编码的,所以这里完全不相关

标签: php html curl utf-8


【解决方案1】:

两个页面都是 UTF-8 编码的,并且 cURL 按原样返回。问题是以下处理;假设涉及 libxml2,它会尝试从 &lt;meta&gt; 元素中猜测编码,但如果没有,则假定为 ISO-8859-1。如果将 UTF-8 BOM ("\xEF\xBB\xBF") 预置到 HTML 中,则可以强制它采用 UTF-8。

【讨论】:

  • 这拯救了我的皮肤。非常感谢
【解决方案2】:

正如@cmb 在上面的答案中所提到的,对于那些希望详细查看我的最终代码的人。给你

$url = "https://stackoverflow.com/
 
$html = str_get_html($url);

libxml_use_internal_errors(true); // Yeah if you are so worried about using @ with warnings

    $doc = new DomDocument();
    $doc->loadHTML("\xEF\xBB\xBF$html"); // This is where and how you put the BOM
    $xpath = new DOMXPath($doc);
    $query = '//*/meta[starts-with(@property, \'og:\')]';
    $metas = $xpath->query($query);
    $rmetas = array();

    foreach ($metas as $meta) {
        $property = $meta->getAttribute('property');
        $content = $meta->getAttribute('content');
        $rmetas[$property] = $content;
    }

    var_dump($rmetas);

希望它可以帮助处于同样危险中的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 2010-10-09
    相关资源
    最近更新 更多