gpt4 book ai didi

php - utf8 到 ISO-8859-1 无法通过 Curl 正确转换某些字符

转载 作者:可可西里 更新时间:2023-11-01 00:17:03 25 4
gpt4 key购买 nike

我有一个采用 UTF8 编码字符的应用程序,需要使用 ISO-8859-1 编码通过 curl 将它们作为 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 但这似乎让事情变得更糟。我错过了什么吗?

谢谢

最佳答案

据我所知,多字节扩展不知道如何音译诸如欧元符号之类的字符,但是 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) 的使用,它将 '€' 字符音译为 Latin-1 等价字符"的 'EUR'。

关于php - utf8 到 ISO-8859-1 无法通过 Curl 正确转换某些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5475179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com