我试图使用php从clickatell短信提供商发送阿拉伯语文本,但我能够发送的只是乱码文本。我用来测试的文字是“غثسهفس”。
我尝试使用iconv将消息文本编码为Windows-1256,1250和ISO-8859-6格式,但每次只发送乱码文本。
任何人都可以给我一些指向我缺少的东西吗?
提前致谢
答案 0 :(得分:3)
好的,这适用于后来走上这条道路的人。找到解决方案here 从Esailija的答案中保留代码
<?php
$text = "غثس هفس خن";
$arr = unpack('H*hex', iconv('UTF-8', 'UCS-2BE', $text));
$message = strtoupper($arr['hex']);
$username = '';
$password = '';
$API_ID = '';
$to = '';
$url = "http://api.clickatell.com/http/auth?user=$username&password=$password&api_id=$API_ID";
$ret = file($url);
$sess = explode(":",$ret[0]);
if ($sess[0] == "OK") {
$sess_id = trim($sess[1]);
$url = "http://api.clickatell.com/http/sendmsg?session_id=$sess_id&to=$to&text=$message&unicode=1";
$ret = file($url);
$send = explode(":",$ret[0]);
if ($send[0] == "ID") {
echo "success - message ID: ". $send[1];
} else {
echo "send message failed";
}
} else {
echo "Authentication failure: ". $ret[0];
}
答案 1 :(得分:1)
基于this,默认为GSM,但您也可以选择UCS2。
所以:
<?php
//Make sure the PHP source file is physically
//saved in utf-8
$text = rawurlencode(iconv( "UTF-8", "UCS-2", "غثس هفس "));
$url = "http://api.clickatell.com/http/auth?user=username&password=password&api_id=API_ID&encoding=UCS2";
$ret = file($url);
$sess = explode(":",$ret[0]);
if ($sess[0] == "OK") {
$sess_id = trim($sess[1]);
$url = "http://api.clickatell.com/http/sendmsg?session_id=$sess_id&to=$to&text=$text&encoding=UCS2";
$ret = file($url);
$send = explode(":",$ret[0]);
if ($send[0] == "ID") {
echo "successnmessage ID: ". $send[1];
} else {
echo "send message failed";
}
} else {
echo "Authentication failure: ". $ret[0];
}
答案 2 :(得分:1)
我为Vtiger 6重写了ClickATell代码,以便它可以容纳UTF-8扩展字符。我用土耳其语。我使用下面的代码进行转换。我希望你可以移植。
/**
* Function to handle UTF-8 Check and conversion
* @author Nuri Unver
*
*/
public function smstxtcode($data){
$mb_hex = '';
$utf = 0;
for($i = 0 ; $i<mb_strlen($data,'UTF-8') ; $i++){
$c = mb_substr($data,$i,1,'UTF-8');
$o = unpack('N',mb_convert_encoding($c,'UCS-4BE','UTF-8'));
$hx = sprintf('%04X',$o[1]);
$utf += intval(substr($hx,0,2));
$mb_hex .= $hx;
}
if ($utf>0)
{
$return=$mb_hex;
$utf=1;
}
else
{
$return=utf8_decode($data);
$utf=0;
}
return array($utf,$return);
}
您将此消息称为此功能。您得到的响应是一个数组,表示根据消息以及要发送的文本发送unicode或普通文本。如果没有扩展字符,它只是以unicode = 0的纯文本形式发送它以保存字符。如果消息包含扩展字符,它会将消息转换为十六进制代码并将其作为unicode发送。
此代码只进行计算。您需要实现自己的代码以将其移植到您的系统。为了演示,这是我用于Vtiger提取数据并发送消息的代码:
/**
* Function to handle SMS Send operation
* @param <String> $message
* @param <Mixed> $toNumbers One or Array of numbers
*/
public function send($message, $toNumbers) {
if(!is_array($toNumbers)) {
$toNumbers = array($toNumbers);
}
$params = $this->prepareParameters();
$smsarray = $this->smstxtcode($message);
$params['text'] = $smsarray[1];
$params['unicode'] = $smsarray[0];
$params['to'] = implode(',', $toNumbers);
$serviceURL = $this->getServiceURL(self::SERVICE_SEND);
答案 3 :(得分:1)
简短回答:你需要做两件事:
a)转换您的阿拉伯语消息:
$ data = iconv(&#34; UTF-8&#34;,&#34; UCS-2BE&#34;,$ data); $ data = bin2hex($ data);
将结果字符串用作消息文本
b)通过HTTP API发送时,请包含unicode参数:
&安培;的unicode = 1