我正在尝试使用PHP从clickatell短信提供商发送阿拉伯文本,但到目前为止我只能发送乱码。我用于测试的文本是"غثس هفس"。
我尝试使用iconv将消息文本编码为Windows-1256、1250和ISO-8859-6格式,但每次都只发送乱码。
请问有人可以给我一些提示吗?
先行致谢。
我正在尝试使用PHP从clickatell短信提供商发送阿拉伯文本,但到目前为止我只能发送乱码。我用于测试的文本是"غثس هفس"。
我尝试使用iconv将消息文本编码为Windows-1256、1250和ISO-8859-6格式,但每次都只发送乱码。
请问有人可以给我一些提示吗?
先行致谢。
好的,这是留给以后遇到此问题的人的。在这里找到了解决方案(点击此处)。 保留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];
}
基于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];
}
我重写了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);
}
/**
* 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);
简短回答:你需要做两件事:
a)转换你的阿拉伯信息:
$data = iconv("UTF-8", "UCS-2BE", $data); $data = bin2hex($data);
使用生成的字符串作为你的信息文本。
b)在通过HTTP API发送时,包括unicode参数:
&unicode=1