3

我有一个使用 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);

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

注意欧元符号€</p>

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

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

谢谢

4

3 回答 3

4

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

<?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)which 将“€”字符音译为“EUR”的拉丁 1“等价物”。

于 2011-03-29T15:47:53.173 回答
3

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

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

于 2011-03-29T15:43:56.813 回答
1

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

于 2011-06-20T12:37:35.090 回答