Php Function to send SMS via Kannel

MaxI picture MaxI · Jan 22, 2011 · Viewed 12.1k times · Source

I can now send an SMS via kannel. However this is done via headers eg:

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

I want to send an sms via a php function and I got the code below online but it doesn't work. (Kannel smsbox log shows no request):

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

}

Is there something wrong? I tried replacing the CONFIG_KANNEL_USER_NAME and the rest with the actual values but it still doesn't work. Open to suggestions.

Answer

MaxI picture MaxI · Jan 24, 2011

I used cURL and it works 100% okay. file_get_contents does not work for me because I want to pass variables to the kannel url and file_get_contents does not process variables coz it insists on using single quotes(php treats it as a string value) instead of double quotes(php will parse the string checking for variables etc). Here is what i am currently doing assuming you already have your variables initialized somewhere:

$textmsg="Hello Stackoverflow Users!";

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

This will work for the simple task of telling kannel to send an sms to a number. Took me a while to realize curl does not recognize spaces and special characters :-).