【问题标题】:utf8 to ISO-8859-1 not converting some characters correctly through Curlutf8 到 ISO-8859-1 无法通过 Curl 正确转换某些字符
【发布时间】:2011-03-29 15:40:28
【问题描述】:

我有一个使用 UTF8 编码字符的应用程序,需要通过 curl 使用 ISO-8859-1 编码将它们作为 xml 的一部分发送。

这是我的测试代码:

header('Content-Type: text/plain; charset=IS0-8859-1');

$message = '§ ° " @ # € % & / ( ) = + ` ´ ^ ¨ * - _ : . ; ,';

echo mb_convert_encoding($message, 'ISO-8859-1', 'UTF-8');

//build xml to post
$content =
    '<?xml version="1.0" encoding="ISO-8859-1"?>
    <mobilectrl_sms>
        <header>
            <customer_id>'.CUSTOMER_ID.'</customer_id>
            <password>'.PASSWORD_ID.'</password>
        </header>
        <payload>
            <sms account="'.SHORT_CODE.'">
                <message><![CDATA['.mb_convert_encoding($message, 'ISO-8859-1', 'UTF-8').']]></message>
                <to_msisdn>+12345678900</to_msisdn>
            </sms>
        </payload>
    </mobilectrl_sms>';

$posturl = MT_URL;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $posturl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml", "Content-length: ".strlen($content), "charset=ISO-8859-1"));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);

在浏览器中它几乎可以工作,我看到 § °" @ # ? % & / ( ) = + ` ´ ^ ¨ * - _ : . ; ,

注意欧元符号€

但是当它以短信的形式出现时,我看到了 § ? " @ # ? % & / ( ) = + ? ? ^ ? * - _ : . ; ,

我想不通,我也尝试过 utf8_decode 但这似乎使情况变得更糟。我错过了什么吗?

谢谢

【问题讨论】:

    标签: php curl utf-8 iso-8859-1


    【解决方案1】:

    AFAIK,多字节扩展不知道如何音译诸如欧元符号之类的字符,但iconv() 知道(来自http://php.net/function.iconv#example-2228 的示例代码):

    <?php
    $text = "This is the Euro symbol '€'.";
    
    echo 'Original : ', $text, PHP_EOL;
    echo 'TRANSLIT : ', iconv("UTF-8", "ISO-8859-1//TRANSLIT", $text), PHP_EOL;
    echo 'IGNORE   : ', iconv("UTF-8", "ISO-8859-1//IGNORE", $text), PHP_EOL;
    echo 'Plain    : ', iconv("UTF-8", "ISO-8859-1", $text), PHP_EOL;
    

    上面的例子会输出类似于:

    Original : This is the Euro symbol '€'.
    TRANSLIT : This is the Euro symbol 'EUR'.
    IGNORE   : This is the Euro symbol ''.
    Plain    :
    Notice: iconv(): Detected an illegal character in input string in .\iconv-example.php on line 7
    This is the Euro symbol '
    

    注意iconv("UTF-8", "ISO-8859-1//TRANSLIT", $text) 的使用,它将“€”字符音译为“EUR”的拉丁语1“等价物”。

    【讨论】:

      【解决方案2】:

      ISO-8859-1 中没有欧元符号,因此用问号代替。除了选择其他东西来代替它之外,您无能为力。

      转换为?s 的其他字符也是如此。

      【讨论】:

      • 这就是为什么 ISO-8859-* 被认为是传统的,而 UTF-8/16 被认为是可用标准中明智和现代的选择。
      • 感谢您的回答,我将不得不考虑将这些字符转换为接近的字符。它用于短信应用程序,显然很多运营商 gsm 仍然使用 iso-8859,这是在欧洲!我想没有人可以给欧元符号发短信。
      【解决方案3】:

      某些 SMS 协议接受欧元符号的“%80”。因此,您可以尝试将“€”替换为“%80”,并使用 ISO-8859-1 对字符串的其余部分进行 URL 编码。它适用于某些 SMS 协议。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-21
        • 1970-01-01
        • 2011-08-26
        • 2016-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-24
        相关资源
        最近更新 更多