utf8到ISO-8859-1没有通过Curl正确转换某些字符

时间:2011-03-29 15:40:29

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

我有一个应用程序正在接受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);

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

注意欧元符号€

但是当我看到它作为短信发出来的时候 §? “@#?%&amp; /()= +??^?* - _:。;,

我无法弄明白,我也尝试过utf8_decode,但这似乎让它变得更糟。我错过了什么吗?

由于

3 个答案:

答案 0 :(得分:4)

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“等价物”。

答案 1 :(得分:3)

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

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

答案 2 :(得分:1)

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