2011-01-22 71 views
1

我现在可以通过kannel发送短信。然而,这是通过头文件来完成,例如:Php通过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和其余的,但它仍然不起作用。接受建议。

+0

`echo'http://'.CONFIG_KANNEL_HOST。':'.CONFIG_KANNEL_PORT。$ url;`并尝试首先请求它 – zerkms 2011-01-22 15:09:42

回答

4

我用卷曲和它的作品100%没问题。 file_get_contents不适用于我,因为我想将变量传递给kannel url,file_get_contents不处理变量,因此它坚持使用单引号(php将其视为字符串值)而不是双引号(php将解析字符串检查变量等)。 这是我目前正在做的假设你已经有你的变量初始化的地方:

$ textmsg =“你好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发送短信到一个数字。花了我一段时间才意识到卷曲不能识别空格和特殊字符:-)。

0

如果您试图在后台触发URL加载(而不是将用户重定向到URL),则需要使用类似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被定义为“本地主机”和CONFIG_KANNEL_PORT被定义为13013那么它有效地做同样的事情,尽管有附加字符集的操作。)

+0

file_get_contents似乎可以工作,只不过它读取$ in_number作为字符串的一部分而不是变量。双引号解决了变量问题,但是然后file_get_contents()需要一个由单引号定义的字符串。有没有办法将变量直接传递给字符串?谢谢 – MaxI 2011-01-23 09:36:46

+0

@Max file_get_contents不需要带单引号的字符串 - 单引号和双引号在PHP中可以互换,只要它们在单个构造中一致使用即可。 (两者之间的区别在于双引号将评估任何嵌入的变量等)。因此,只需将单引号更改为双引号,并且一切都应该很好。 :-) – 2011-01-23 10:18:08

+0

相信我。它们不可互换。我试过了。尝试使用cURL – MaxI 2011-01-24 04:29:34

1

我和赞比亚恩多拉的朋友使用ubuntu 11.04运行kannel_1.4.3。它发送和接收短信完美的方式。下面的代码必须进行编辑才能发送更多70个字符。我的朋友和我努力弄清楚'& charset = UCS-2 &'编码= 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)); 
1

使用curl:

$gw_host=127.0.0.1 
$gw_port=13xx3 

等:

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"); 

各种变量/参数与你的价值观,如更换

0

不要ressucitate一个古老的问题,但对于后天性和其他人搜索同样的事情:

[[email protected] 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:

[[email protected] 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"; 
    } 

    ?> 

这一个兼作短信接收器和短信发送通知接收器(在这种情况下,它会更新发送短信时确认接收到的数据库中的记录)。

它用于DLR,因为我在发送SMS时发送URL(并设置DLR掩码要求确认),但对于入站SMS,您必须配置您的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 
    [...] 

对不起,葡萄牙语一些文本,但你可以得到的图片

0

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