如何在SMS(Twilio,Plivo)中使用unicodes或特殊字符

时间:2018-03-09 13:18:49

标签: twilio sms-gateway plivo

如果我必须在消息内容的开头发送带有两个空格的短信,如下所示

  setparam 3003:70eac;

这适用于某些设备配置。但收到短信的时候是

setparam 3003:70eac;

空白消失了。 这是我用PHP帮助库发送短信的方式

$client = new Client($account_sid, $auth_token);
$client->messages->create(
    '<to_number>',
    array(
        'from' => '<from_number>',
        'body' => '  setparam 3003:70eac;'
    )
);

我还试过发送如下编码

%20%20setparam%203003%3A70eac%3B

此结果无需解析

%20%20setparam%203003%3A70eac%3B

任何帮助都会给我很大的帮助。

2 个答案:

答案 0 :(得分:2)

在Twilio或Plivo中发送短信时,我们无法正常使用whitspace,因为这些是自动修剪的。但是可以通过转发消息中的unicode value空格来发送。

<强>示例:

  

的Python

from twilio.rest import Client

# Your Account SID from twilio.com/console
account_sid = "<twilio_sid>"
# Your Auth Token from twilio.com/console
auth_token  = "<twilio_token>"

client = Client(account_sid, auth_token)

message = client.messages.create(
    to="<to_number>",
    from_="<from_number>",
    body="\U00002001\U00002001Whitspace at front")

print(message.sid)
  

JSON

{
"src" : "<from_number>",
"dst" : "<to_number>", 
"text" : "\u0020\u0020Whitspace at front", 
"type" : "sms", 
"method" : "POST" 
}
  

PHP

<?php
// Required if your environment does not handle autoloading
require __DIR__ . '/vendor/autoload.php';

// Use the REST API Client to make requests to the Twilio REST API
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$sid = 'ACfe1c3f0e87c51710e95e842f2e71922b';
$token = 'your_auth_token';
$client = new Client($sid, $token);

// Use the client to do fun stuff like send text messages!
$client->messages->create(
    // the number you'd like to send the message to
    '+15558675309',
    array(
        // A Twilio phone number you purchased at twilio.com/console
        'from' => '+15017250604',
        // the body of the text message you'd like to send
        'body' => "\020\020Whitspace at front"
    )
);

您可以看到邮件正文用双引号(“)表示。这在PHP中很重要。使用单引号(')表示不起作用。The different quote usages in PHPDouble quoted strings

PHP的更多参考链接。

<强>结果:

'  Whitspace at front'

答案 1 :(得分:0)

在twilio中,您将必须使用Unicode'Punctuation Space',即'\ 2008'。 '\ 0020'无法正常工作。