Php功能通过Kannel发送短信

时间:2011-01-22 15:01:18

标签: php linux function kannel

我现在可以通过kannel发送短信了。然而,这是通过标题来完成的,例如:

header("Location:http://localhost:13013/cgi-bin/sendsms?username=xxxx&password=xxxx&to=$in_number&text=$in_msg");  

我想通过php函数发送短信,我在网上得到了以下代码,但它不起作用。 (Kannel smsbox日志没有显示请求):

  function sendSmsMessage($in_number, $in_msg)
 {
 $url = '/cgi-bin/sendsms?username=' . CONFIG_KANNEL_USER_NAME
 . '&password=' . CONFIG_KANNEL_PASSWORD    
 . '&charset=UCS-2&coding=2'
 . "&to={$in_number}"
 . '&text=' . urlencode(iconv('utf-8', 'ucs-2', $in_msg));



$results = file('http://'  
                 . CONFIG_KANNEL_HOST . ':'
                 . CONFIG_KANNEL_PORT . $url);

}

有什么问题吗?我尝试用实际值替换CONFIG_KANNEL_USER_NAME和其余部分,但它仍然无效。接受建议。

6 个答案:

答案 0 :(得分:4)

我使用了cURL,它100%正常工作。 file_get_contents对我不起作用,因为我想将变量传递给kannel url而file_get_contents不处理变量因为它坚持使用单引号(php将其视为字符串值)而不是双引号(php将解析字符串检查变量等)。 这是我目前正在做的假设您已经在某处初始化变量:

$ textmsg =“Hello Stackoverflow用户!”;

$ cellphone_number =“+ 254xxxxxxx”

$ encmsg =用urlencode($ textmsg);

$ch= curl_init();
curl_setopt($ch, "http://localhost:13013/cgi-bin/sendsms?username=xxxxx&password=xxxxx&to=$cellphone_number&text=$encmsg");
curl_exec($ch);
curl_close($ch);

这适用于告诉kannel将短信发送到号码的简单任务。花了一些时间才意识到卷曲不能识别空格和特殊字符: - )。

答案 1 :(得分:1)

来自赞比亚恩多拉的我和朋友正在使用ubuntu 11.04来运行kannel_1.4.3。它在发送和接收短信方面非常有效。必须编辑下面的代码才能发送超过70个字符。我的朋友和我努力弄清楚'& charset = UCS-2& coding = 2'这一行有一个小错误。正确的行应该是'& charset = UCS-2& encoding = 2'。因此代码应如下所示:

 function sendSmsMessage($in_number, $in_msg)
 {
 $url = '/cgi-bin/sendsms?username=' . CONFIG_KANNEL_USER_NAME
 . '&password=' . CONFIG_KANNEL_PASSWORD    
 . '&charset=UCS-2&encoding=2'
 . "&to={$in_number}"
 . '&text=' . urlencode(iconv('utf-8', 'ucs-2', $in_msg));

答案 2 :(得分:1)

使用curl:

curl_init("http://$gw_host:$gw_port/cgi-bin/sendsms?username=$gw_user&password=$gw_pass&to=$to&from=$shortcode&smsc=$smsc&dlr-mask=$dlrmask&binfo=$shortcode&text=$message");

用您的值替换各种变量/参数,例如:

$gw_host=127.0.0.1
$gw_port=13xx3

答案 3 :(得分:0)

如果您尝试在后台触发网址加载(而不是将用户重定向到网址),则需要使用cURL或甚至file_get_contents之类的内容。

例如,如果您的设置启用了fopen URL包装器,则只需使用:

$response = file_get_contents("http://localhost:13013/cgi-bin/sendsms?username=xxxx&password=xxxx&to=$in_number&text=$in_msg");

无论如何,如果没有一些额外的调试信息,很难知道为什么你找到的功能不起作用。 (如果将CONFIG_KANNEL_HOST定义为“localhost”并将CONFIG_KANNEL_PORT定义为13013,则它实际上会执行相同的操作,尽管还有其他字符集操作。)

答案 4 :(得分:0)

不是为了重新提出一个古老的问题,而是为了后天和其他人寻找同样的事情:

    [root@sat2 tools]# cat kannel-send.php 
    <?php

    function send_sms($msgid, $numto, $msgtext, $smsc = "smsc-default", $dlrmask = 63)
    {

            $sendsmsurl_prefix = "http://localhost:13013/cgi-bin/sendsms";
            $dlrurl_prefix = "http://localhost/tools/kannel-receive.php";
            $username = "user";
            $password = "pass";

            # fix number to what carriers expect
            $numto = preg_replace('/^0/', '', $numto);
            $numto = preg_replace('/^\+55/', '', $numto);
            $numto = "0" . $numto;

            if (!$msgid) $dlrmask = 0;

            $dlrurl_params = array(
                    "type" => "dlr",
                    "timesent" => "%t",
                    "smsc" => "%i",
                    "uuid" => "%I",
                    "fid" => "%F",
                    "dlr-cod" => "%d",
                    "reply" => "%A",
                    "msgid" => $msgid,
                    "text" => "%a",
                    "to" => "%P",
                    "from" => "%p",
                    "origsmsc" => "%f",
            );

            $dlrurl = $dlrurl_prefix . "?" . urldecode(http_build_query($dlrurl_params));

            $sendsmsurl_params = array(
                    "username" => $username,
                    "password" => $password,
                    "to" => $numto,
                    "dlr-mask" => $dlrmask,
                    "dlr-url" => $dlrurl,
                    "smsc"=> $smsc,
                    "text" => $msgtext,
            );

            $sendsmsurl = $sendsmsurl_prefix . "?" . http_build_query($sendsmsurl_params);

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_URL, $sendsmsurl);
            $bogus = curl_exec($ch);
            $ret = curl_error($ch);
            curl_close($ch);

            return $ret == "";

    }

    ?>

你可以让另外一个接收短信并将其存储到mysql:

    [root@sat2 tools]# cat kannel-receive.php 
    <?php

    $debug = false;

    $link = null;

    function dbconnect()
    {
            global $link, $debug;

            if ($link && mysql_ping($link))
                    return;

            if ($debug) echo "Conectando ao banco de dados\n";
            // TODO: criar um usuario de banco especifico pra isso
            $host = 'localhost';
            $user = 'user';
            $pass = 'pass';
            $db = 'dbname';
            $link = mysql_connect($host, $user, $pass, true);
            if (!$link){ 
                    if ($debug) echo "Can't connect to mysql: " . mysql_error() . "\n";
            } else {
                    mysql_select_db($db, $link);
            }
            return;
    }

    function esc($str)
    {
            global $link;
            return mysql_real_escape_string($str, $link);
    }

    if ($debug) {
            echo "<br>Kannel inbound sms event:<br>\n";
            var_dump($_GET);
    }

    dbconnect();

    if ($_GET['type'] == "inbsms") {

            $_GET['from'] = preg_replace('/^(\+55|0)/', '', $_GET['from']);
            $sql = "INSERT INTO notificacao (tipo, endereco, mensagem, device,
                            dataEvento, situacao)
                    VALUES ('%s', '%s','%s','%s','%s','%s')";
            $sql = sprintf($sql, 'sms', esc($_GET['from']), esc($_GET['text']),
                    esc($_GET['smsc']), esc($_GET['timesent']), "received");

    } elseif ($_GET['type'] == "dlr") {

            switch (esc($_GET['dlr-cod'])) {
            case "1":
                    $sql = "UPDATE notificacao SET 
                                    situacao = 'confirmed',
                                    dataConfirmacao = '{$_GET['timesent']}'
                            WHERE idnotificacao = {$_GET['msgid']}";
                    break;
            case "8":
                    $sql = "UPDATE notificacao SET 
                                    situacao = 'sent',
                                    device = '{$_GET['smsc']}',
                                    dataEvento = '{$_GET['timesent']}'
                            WHERE idnotificacao = {$_GET['msgid']}";
                    break;
            case "16":
                    $sql = "UPDATE notificacao SET 
                                    situacao = 'failed',
                                    device = '{$_GET['smsc']}',
                                    razaofalha = '{$_GET['reply']}',
                                    dataEvento = '{$_GET['timesent']}'
                            WHERE idnotificacao = {$_GET['msgid']}";
                    break;
            }

    }

    if ($debug) echo "sql: $sql\n";

    $result = mysql_query($sql, $link);
    if (!$result) {
            if ($debug) echo "Erro sql: " . mysql_error() . "\n";
    }

    ?>

这个可以兼作SMS接收器和SMS-Delivery-Notification接收器(在这种情况下,它会更新发送短信时放在那里的数据库上的记录,以确认它已被接收)。

它用于DLR,因为我在发送短信时发送了该URL(并设置了DLR掩码请求确认),但是对于入站短信,你必须配置你的kannel.conf来使用它(你可以有很多短信 - 服务,这只是一个全能的例子:

    [...]
    group = sms-service
    keyword = default
    get-url = "http://localhost/tools/kannel-receive.php?type=inbsms&text=%a&timesent=%t&from=%p&to=%P&smsc=%i&uuid=%I&delivery=%d&service=%n&encoding=%c&class=%m&mwi=%M&charset=%C&udh=%u&dcs=%O&origsmsc=%f"
    catch-all = yes
    max-messages = 0
    accept-x-kannel-headers = true
    concatenation = yes
    omit-empty = yes
    [...]

对于葡萄牙语的一些文本感到抱歉,但你可以得到图片。

答案 5 :(得分:0)

// php代码文件名是test2.php.before,你必须在ubuntu14上安装php5-curl