【发布时间】:2021-10-27 15:23:18
【问题描述】:
我正在从两个网站的 cURL 获取 HTML。
站点 1: https://xperia.sony.jp/campaign/360RA/?s_tc=somc_co_ext_docomo_360RA_banner
我的 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。一个有效,另一个显示方形框。
我也试过
https://www.php.net/manual/en/function.mb-convert-encoding.php
还有更多
我花费了关键的时间来解决这个问题。欢迎任何想法
【问题讨论】:
-
xperia 网站包含明确的
<head> <meta charset="utf-8"> …而fidelity 没有? -
一种将两者都编码为 UTF-8 的方法?你!
-
我仍然可以看到链接ctrlv.link/CV8A 添加
CURLOPT_ENCODING => 'UTF-8' -
CURLOPT_ENCODING 是关于内容编码的,所以这里完全不相关