Php Function to send SMS via Kannel
Solution 1
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 :-).
Solution 2
My friend and I from Ndola, Zambia are using ubuntu 11.04 to run kannel_1.4.3. It works perfectly way in sending and receiving sms. The code below had to be edited for it to send more that 70 characters. My friend and I struggled to figure out that there was a small error in the line '&charset=UCS-2&coding=2'. The correct line should be '&charset=UCS-2&encoding=2'. So the code should appear as below:
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));
Solution 3
Using 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");
Replace the various variables/parameters with your values such as:
$gw_host=127.0.0.1
$gw_port=13xx3
etc.
MaxI
Embrace the weird. JavaEE for life! I mostly ask questions on SO and read various posts to enhance my knowledge. I relax by coding!! and gaming.
Updated on June 13, 2022Comments
-
MaxI almost 2 years
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.
-
MaxI over 13 yearsThat file_get_contents seems to work except that it reads the $in_number as part of the string instead of a variable. double quotes solves the variable issue but then file_get_contents() requires a string which is defined by single quotes. Is there a way to pass the variables directly to the string? thanks
-
John Parker over 13 years@Max file_get_contents doesn't require a string with single quotes - single and double quotes are interchangeable in PHP, as long as they're used consistently within a single construct. (The different between the two being that double quotes will evaluate any embedded variables, etc.) As such, simply change the single quotes to double quotes and all should be well. :-)
-
MaxI over 13 yearsTrust me. They aren't interchangeable. I've tried. Trying to use cURL
-
MaxI over 13 yearsYou need to have curl installed and php running with cURL support. For linux you do sudo apt-get install php5-curl. if you check phpinfo() you should see curl somewhere.
-
MaxI over 12 yearsI'm from Kenya and I use UBUNTU 10.04.3. I'll definately try this function
-
Sam over 5 yearscurl_setopt() expects exactly 3 parameters, 2 given. This is the error I am getting.